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 * Allocates memory for an encoding code block.
125 static opj_bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block);
129 Free the memory allocated for encoding
130 @param tcd TCD handle
132 static void tcd_free_tile(opj_tcd_v2_t *tcd);
135 opj_bool tcd_t2_decode (
137 OPJ_BYTE * p_src_data,
138 OPJ_UINT32 * p_data_read,
139 OPJ_UINT32 p_max_src_size,
140 opj_codestream_index_t *p_cstr_index
143 opj_bool tcd_t1_decode (
147 opj_bool tcd_dwt_decode (
151 opj_bool tcd_mct_decode (
155 opj_bool tcd_dc_level_shift_decode (
159 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct);
160 /* ----------------------------------------------------------------------- */
163 Create a new TCD handle
165 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
166 /* create the tcd structure */
167 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
168 if(!tcd) return NULL;
170 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
171 if(!tcd->tcd_image) {
180 Create a new TCD handle
182 opj_tcd_v2_t* tcd_create_v2(opj_bool p_is_decoder)
184 opj_tcd_v2_t *l_tcd = 00;
186 /* create the tcd structure */
187 l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t));
191 memset(l_tcd,0,sizeof(opj_tcd_v2_t));
193 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
195 l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t));
196 if (!l_tcd->tcd_image) {
200 memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t));
206 Destroy a previously created TCD handle
208 void tcd_destroy(opj_tcd_t *tcd) {
210 opj_free(tcd->tcd_image);
215 /* ----------------------------------------------------------------------- */
217 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
218 int tileno, compno, resno, bandno, precno, cblkno;
222 tcd->tcd_image->tw = cp->tw;
223 tcd->tcd_image->th = cp->th;
224 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
226 for (tileno = 0; tileno < 1; tileno++) {
227 opj_tcp_t *tcp = &cp->tcps[curtileno];
230 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
231 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
232 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
234 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
235 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
237 /* 4 borders of the tile rescale on the image if necessary */
238 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
239 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
240 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
241 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
242 tile->numcomps = image->numcomps;
243 /* tile->PPT=image->PPT; */
245 /* Modification of the RATE >> */
246 for (j = 0; j < tcp->numlayers; j++) {
247 tcp->rates[j] = tcp->rates[j] ?
249 (((float) (tile->numcomps
250 * (tile->x1 - tile->x0)
251 * (tile->y1 - tile->y0)
252 * image->comps[0].prec))
253 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
255 ((float) (tile->numcomps
256 * (tile->x1 - tile->x0)
257 * (tile->y1 - tile->y0)
258 * image->comps[0].prec))/
259 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
263 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
264 tcp->rates[j] = tcp->rates[j - 1] + 20;
266 if (!j && tcp->rates[j] < 30)
270 if(j == (tcp->numlayers-1)){
271 tcp->rates[j] = tcp->rates[j]- 2;
275 /* << Modification of the RATE */
277 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
278 for (compno = 0; compno < tile->numcomps; compno++) {
279 opj_tccp_t *tccp = &tcp->tccps[compno];
281 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
283 /* border of each tile component (global) */
284 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
285 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
286 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
287 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
289 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
290 tilec->numresolutions = tccp->numresolutions;
292 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
294 for (resno = 0; resno < tilec->numresolutions; resno++) {
296 int levelno = tilec->numresolutions - 1 - resno;
297 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
298 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
299 int cbgwidthexpn, cbgheightexpn;
300 int cblkwidthexpn, cblkheightexpn;
302 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
304 /* border for each resolution level (global) */
305 res->x0 = int_ceildivpow2(tilec->x0, levelno);
306 res->y0 = int_ceildivpow2(tilec->y0, levelno);
307 res->x1 = int_ceildivpow2(tilec->x1, levelno);
308 res->y1 = int_ceildivpow2(tilec->y1, levelno);
310 res->numbands = resno == 0 ? 1 : 3;
311 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
312 if (tccp->csty & J2K_CCP_CSTY_PRT) {
313 pdx = tccp->prcw[resno];
314 pdy = tccp->prch[resno];
319 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
320 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
321 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
323 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
324 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
326 res->pw = (brprcxend - tlprcxstart) >> pdx;
327 res->ph = (brprcyend - tlprcystart) >> pdy;
330 tlcbgxstart = tlprcxstart;
331 tlcbgystart = tlprcystart;
332 brcbgxend = brprcxend;
333 brcbgyend = brprcyend;
337 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
338 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
339 brcbgxend = int_ceildivpow2(brprcxend, 1);
340 brcbgyend = int_ceildivpow2(brprcyend, 1);
341 cbgwidthexpn = pdx - 1;
342 cbgheightexpn = pdy - 1;
345 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
346 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
348 for (bandno = 0; bandno < res->numbands; bandno++) {
351 opj_stepsize_t *ss = NULL;
353 opj_tcd_band_t *band = &res->bands[bandno];
355 band->bandno = resno == 0 ? 0 : bandno + 1;
356 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
357 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
359 if (band->bandno == 0) {
360 /* band border (global) */
361 band->x0 = int_ceildivpow2(tilec->x0, levelno);
362 band->y0 = int_ceildivpow2(tilec->y0, levelno);
363 band->x1 = int_ceildivpow2(tilec->x1, levelno);
364 band->y1 = int_ceildivpow2(tilec->y1, levelno);
366 /* band border (global) */
367 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
368 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
369 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
370 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
373 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
374 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
375 numbps = image->comps[compno].prec + gain;
377 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
378 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
380 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
382 for (i = 0; i < res->pw * res->ph * 3; i++) {
383 band->precincts[i].imsbtree = NULL;
384 band->precincts[i].incltree = NULL;
385 band->precincts[i].cblks.enc = NULL;
388 for (precno = 0; precno < res->pw * res->ph; precno++) {
389 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
391 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
392 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
393 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
394 int cbgyend = cbgystart + (1 << cbgheightexpn);
396 opj_tcd_precinct_t *prc = &band->precincts[precno];
398 /* precinct size (global) */
399 prc->x0 = int_max(cbgxstart, band->x0);
400 prc->y0 = int_max(cbgystart, band->y0);
401 prc->x1 = int_min(cbgxend, band->x1);
402 prc->y1 = int_min(cbgyend, band->y1);
404 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
405 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
406 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
407 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
408 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
409 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
411 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
412 prc->incltree = tgt_create(prc->cw, prc->ch);
413 prc->imsbtree = tgt_create(prc->cw, prc->ch);
415 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
416 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
417 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
418 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
419 int cblkyend = cblkystart + (1 << cblkheightexpn);
421 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
423 /* code-block size (global) */
424 cblk->x0 = int_max(cblkxstart, prc->x0);
425 cblk->y0 = int_max(cblkystart, prc->y0);
426 cblk->x1 = int_min(cblkxend, prc->x1);
427 cblk->y1 = int_min(cblkyend, prc->y1);
428 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
429 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
431 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
432 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
440 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
443 void tcd_free_encode(opj_tcd_t *tcd) {
444 int tileno, compno, resno, bandno, precno, cblkno;
446 for (tileno = 0; tileno < 1; tileno++) {
447 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
449 for (compno = 0; compno < tile->numcomps; compno++) {
450 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
452 for (resno = 0; resno < tilec->numresolutions; resno++) {
453 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
455 for (bandno = 0; bandno < res->numbands; bandno++) {
456 opj_tcd_band_t *band = &res->bands[bandno];
458 for (precno = 0; precno < res->pw * res->ph; precno++) {
459 opj_tcd_precinct_t *prc = &band->precincts[precno];
461 if (prc->incltree != NULL) {
462 tgt_destroy(prc->incltree);
463 prc->incltree = NULL;
465 if (prc->imsbtree != NULL) {
466 tgt_destroy(prc->imsbtree);
467 prc->imsbtree = NULL;
469 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
470 opj_free(prc->cblks.enc[cblkno].data - 2);
471 opj_free(prc->cblks.enc[cblkno].layers);
472 opj_free(prc->cblks.enc[cblkno].passes);
474 opj_free(prc->cblks.enc);
476 opj_free(band->precincts);
477 band->precincts = NULL;
480 opj_free(tilec->resolutions);
481 tilec->resolutions = NULL;
483 opj_free(tile->comps);
486 opj_free(tcd->tcd_image->tiles);
487 tcd->tcd_image->tiles = NULL;
490 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
491 int tileno, compno, resno, bandno, precno, cblkno;
493 for (tileno = 0; tileno < 1; tileno++) {
494 opj_tcp_t *tcp = &cp->tcps[curtileno];
496 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
497 int p = curtileno % cp->tw;
498 int q = curtileno / cp->tw;
500 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
502 /* 4 borders of the tile rescale on the image if necessary */
503 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
504 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
505 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
506 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
508 tile->numcomps = image->numcomps;
509 /* tile->PPT=image->PPT; */
511 /* Modification of the RATE >> */
512 for (j = 0; j < tcp->numlayers; j++) {
513 tcp->rates[j] = tcp->rates[j] ?
515 (((float) (tile->numcomps
516 * (tile->x1 - tile->x0)
517 * (tile->y1 - tile->y0)
518 * image->comps[0].prec))
519 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
521 ((float) (tile->numcomps
522 * (tile->x1 - tile->x0)
523 * (tile->y1 - tile->y0)
524 * image->comps[0].prec))/
525 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
529 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
530 tcp->rates[j] = tcp->rates[j - 1] + 20;
532 if (!j && tcp->rates[j] < 30)
537 /* << Modification of the RATE */
539 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
540 for (compno = 0; compno < tile->numcomps; compno++) {
541 opj_tccp_t *tccp = &tcp->tccps[compno];
543 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
545 /* border of each tile component (global) */
546 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
547 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
548 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
549 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
551 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
552 tilec->numresolutions = tccp->numresolutions;
553 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
554 for (resno = 0; resno < tilec->numresolutions; resno++) {
557 int levelno = tilec->numresolutions - 1 - resno;
558 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
559 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
560 int cbgwidthexpn, cbgheightexpn;
561 int cblkwidthexpn, cblkheightexpn;
563 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
565 /* border for each resolution level (global) */
566 res->x0 = int_ceildivpow2(tilec->x0, levelno);
567 res->y0 = int_ceildivpow2(tilec->y0, levelno);
568 res->x1 = int_ceildivpow2(tilec->x1, levelno);
569 res->y1 = int_ceildivpow2(tilec->y1, levelno);
570 res->numbands = resno == 0 ? 1 : 3;
572 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
573 if (tccp->csty & J2K_CCP_CSTY_PRT) {
574 pdx = tccp->prcw[resno];
575 pdy = tccp->prch[resno];
580 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
581 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
582 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
583 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
584 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
586 res->pw = (brprcxend - tlprcxstart) >> pdx;
587 res->ph = (brprcyend - tlprcystart) >> pdy;
590 tlcbgxstart = tlprcxstart;
591 tlcbgystart = tlprcystart;
592 brcbgxend = brprcxend;
593 brcbgyend = brprcyend;
597 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
598 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
599 brcbgxend = int_ceildivpow2(brprcxend, 1);
600 brcbgyend = int_ceildivpow2(brprcyend, 1);
601 cbgwidthexpn = pdx - 1;
602 cbgheightexpn = pdy - 1;
605 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
606 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
608 for (bandno = 0; bandno < res->numbands; bandno++) {
611 opj_stepsize_t *ss = NULL;
613 opj_tcd_band_t *band = &res->bands[bandno];
615 band->bandno = resno == 0 ? 0 : bandno + 1;
616 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
617 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
619 if (band->bandno == 0) {
621 band->x0 = int_ceildivpow2(tilec->x0, levelno);
622 band->y0 = int_ceildivpow2(tilec->y0, levelno);
623 band->x1 = int_ceildivpow2(tilec->x1, levelno);
624 band->y1 = int_ceildivpow2(tilec->y1, levelno);
626 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
627 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
628 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
629 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
632 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
633 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
634 numbps = image->comps[compno].prec + gain;
635 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
636 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
638 for (precno = 0; precno < res->pw * res->ph; precno++) {
639 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
641 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
642 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
643 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
644 int cbgyend = cbgystart + (1 << cbgheightexpn);
646 opj_tcd_precinct_t *prc = &band->precincts[precno];
648 /* precinct size (global) */
649 prc->x0 = int_max(cbgxstart, band->x0);
650 prc->y0 = int_max(cbgystart, band->y0);
651 prc->x1 = int_min(cbgxend, band->x1);
652 prc->y1 = int_min(cbgyend, band->y1);
654 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
655 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
656 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
657 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
658 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
659 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
661 opj_free(prc->cblks.enc);
662 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
664 if (prc->incltree != NULL) {
665 tgt_destroy(prc->incltree);
667 if (prc->imsbtree != NULL) {
668 tgt_destroy(prc->imsbtree);
671 prc->incltree = tgt_create(prc->cw, prc->ch);
672 prc->imsbtree = tgt_create(prc->cw, prc->ch);
674 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
675 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
676 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
677 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
678 int cblkyend = cblkystart + (1 << cblkheightexpn);
680 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
682 /* code-block size (global) */
683 cblk->x0 = int_max(cblkxstart, prc->x0);
684 cblk->y0 = int_max(cblkystart, prc->y0);
685 cblk->x1 = int_min(cblkxend, prc->x1);
686 cblk->y1 = int_min(cblkyend, prc->y1);
687 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
688 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
690 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
691 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
699 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
702 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
703 int i, j, tileno, p, q;
704 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
707 tcd->tcd_image->tw = cp->tw;
708 tcd->tcd_image->th = cp->th;
709 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
712 Allocate place to store the decoded data = final image
713 Place limited by the tile really present in the codestream
716 for (j = 0; j < cp->tileno_size; j++) {
717 opj_tcd_tile_t *tile;
719 tileno = cp->tileno[j];
720 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
721 tile->numcomps = image->numcomps;
722 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
725 for (i = 0; i < image->numcomps; i++) {
726 for (j = 0; j < cp->tileno_size; j++) {
727 opj_tcd_tile_t *tile;
728 opj_tcd_tilecomp_t *tilec;
730 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
732 tileno = cp->tileno[j];
734 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
735 tilec = &tile->comps[i];
737 p = tileno % cp->tw; /* si numerotation matricielle .. */
738 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
740 /* 4 borders of the tile rescale on the image if necessary */
741 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
742 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
743 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
744 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
746 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
747 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
748 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
749 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
751 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
752 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
753 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
754 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
757 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
758 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
760 image->comps[i].w = w;
761 image->comps[i].h = h;
762 image->comps[i].x0 = x0;
763 image->comps[i].y0 = y0;
767 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) {
768 int compno, resno, bandno, precno, cblkno;
770 opj_tcd_tile_t *tile;
772 OPJ_ARG_NOT_USED(cstr_info);
776 tcp = &(cp->tcps[cp->tileno[tileno]]);
777 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
779 tileno = cp->tileno[tileno];
781 for (compno = 0; compno < tile->numcomps; compno++) {
782 opj_tccp_t *tccp = &tcp->tccps[compno];
783 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
785 /* border of each tile component (global) */
786 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
787 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
788 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
789 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
791 tilec->numresolutions = tccp->numresolutions;
792 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
794 for (resno = 0; resno < tilec->numresolutions; resno++) {
796 int levelno = tilec->numresolutions - 1 - resno;
797 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
798 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
799 int cbgwidthexpn, cbgheightexpn;
800 int cblkwidthexpn, cblkheightexpn;
802 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
804 /* border for each resolution level (global) */
805 res->x0 = int_ceildivpow2(tilec->x0, levelno);
806 res->y0 = int_ceildivpow2(tilec->y0, levelno);
807 res->x1 = int_ceildivpow2(tilec->x1, levelno);
808 res->y1 = int_ceildivpow2(tilec->y1, levelno);
809 res->numbands = resno == 0 ? 1 : 3;
811 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
812 if (tccp->csty & J2K_CCP_CSTY_PRT) {
813 pdx = tccp->prcw[resno];
814 pdy = tccp->prch[resno];
820 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
821 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
822 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
823 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
824 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
826 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
827 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
830 tlcbgxstart = tlprcxstart;
831 tlcbgystart = tlprcystart;
832 brcbgxend = brprcxend;
833 brcbgyend = brprcyend;
837 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
838 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
839 brcbgxend = int_ceildivpow2(brprcxend, 1);
840 brcbgyend = int_ceildivpow2(brprcyend, 1);
841 cbgwidthexpn = pdx - 1;
842 cbgheightexpn = pdy - 1;
845 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
846 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
848 for (bandno = 0; bandno < res->numbands; bandno++) {
851 opj_stepsize_t *ss = NULL;
853 opj_tcd_band_t *band = &res->bands[bandno];
854 band->bandno = resno == 0 ? 0 : bandno + 1;
855 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
856 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
858 if (band->bandno == 0) {
859 /* band border (global) */
860 band->x0 = int_ceildivpow2(tilec->x0, levelno);
861 band->y0 = int_ceildivpow2(tilec->y0, levelno);
862 band->x1 = int_ceildivpow2(tilec->x1, levelno);
863 band->y1 = int_ceildivpow2(tilec->y1, levelno);
865 /* band border (global) */
866 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
867 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
868 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
869 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
872 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
873 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
874 numbps = image->comps[compno].prec + gain;
875 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
876 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
878 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
880 for (precno = 0; precno < res->pw * res->ph; precno++) {
881 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
882 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
883 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
884 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
885 int cbgyend = cbgystart + (1 << cbgheightexpn);
887 opj_tcd_precinct_t *prc = &band->precincts[precno];
888 /* precinct size (global) */
889 prc->x0 = int_max(cbgxstart, band->x0);
890 prc->y0 = int_max(cbgystart, band->y0);
891 prc->x1 = int_min(cbgxend, band->x1);
892 prc->y1 = int_min(cbgyend, band->y1);
894 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
895 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
896 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
897 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
898 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
899 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
901 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
903 prc->incltree = tgt_create(prc->cw, prc->ch);
904 prc->imsbtree = tgt_create(prc->cw, prc->ch);
906 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
907 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
908 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
909 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
910 int cblkyend = cblkystart + (1 << cblkheightexpn);
912 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
915 /* code-block size (global) */
916 cblk->x0 = int_max(cblkxstart, prc->x0);
917 cblk->y0 = int_max(cblkystart, prc->y0);
918 cblk->x1 = int_min(cblkxend, prc->x1);
919 cblk->y1 = int_min(cblkyend, prc->y1);
926 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
929 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
930 int compno, resno, bandno, precno, cblkno;
931 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
932 int matrice[10][10][3];
935 opj_cp_t *cp = tcd->cp;
936 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
937 opj_tcp_t *tcd_tcp = tcd->tcp;
939 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
941 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
942 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
943 for (i = 0; i < tcd_tcp->numlayers; i++) {
944 for (j = 0; j < tilec->numresolutions; j++) {
945 for (k = 0; k < 3; k++) {
947 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
948 * (float) (tcd->image->comps[compno].prec / 16.0));
953 for (resno = 0; resno < tilec->numresolutions; resno++) {
954 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
955 for (bandno = 0; bandno < res->numbands; bandno++) {
956 opj_tcd_band_t *band = &res->bands[bandno];
957 for (precno = 0; precno < res->pw * res->ph; precno++) {
958 opj_tcd_precinct_t *prc = &band->precincts[precno];
959 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
960 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
961 opj_tcd_layer_t *layer = &cblk->layers[layno];
963 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
964 /* Correction of the matrix of coefficient to include the IMSB information */
966 value = matrice[layno][resno][bandno];
973 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
974 if (imsb >= matrice[layno - 1][resno][bandno]) {
975 value -= (imsb - matrice[layno - 1][resno][bandno]);
983 cblk->numpassesinlayers = 0;
986 n = cblk->numpassesinlayers;
987 if (cblk->numpassesinlayers == 0) {
989 n = 3 * value - 2 + cblk->numpassesinlayers;
991 n = cblk->numpassesinlayers;
994 n = 3 * value + cblk->numpassesinlayers;
997 layer->numpasses = n - cblk->numpassesinlayers;
999 if (!layer->numpasses)
1002 if (cblk->numpassesinlayers == 0) {
1003 layer->len = cblk->passes[n - 1].rate;
1004 layer->data = cblk->data;
1006 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1007 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1010 cblk->numpassesinlayers = n;
1018 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
1020 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1021 tcd_makelayer_fixed(tcd, layno, 1);
1025 void tcd_rateallocate_fixed_v2(opj_tcd_v2_t *tcd) {
1028 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1029 tcd_makelayer_fixed_v2(tcd, layno, 1);
1033 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
1034 int compno, resno, bandno, precno, cblkno, passno;
1036 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1038 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
1040 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1041 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1042 for (resno = 0; resno < tilec->numresolutions; resno++) {
1043 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1044 for (bandno = 0; bandno < res->numbands; bandno++) {
1045 opj_tcd_band_t *band = &res->bands[bandno];
1046 for (precno = 0; precno < res->pw * res->ph; precno++) {
1047 opj_tcd_precinct_t *prc = &band->precincts[precno];
1048 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1049 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1050 opj_tcd_layer_t *layer = &cblk->layers[layno];
1054 cblk->numpassesinlayers = 0;
1056 n = cblk->numpassesinlayers;
1057 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1060 opj_tcd_pass_t *pass = &cblk->passes[passno];
1063 dd = pass->distortiondec;
1065 dr = pass->rate - cblk->passes[n - 1].rate;
1066 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1073 if (dd / dr >= thresh)
1076 layer->numpasses = n - cblk->numpassesinlayers;
1078 if (!layer->numpasses) {
1082 if (cblk->numpassesinlayers == 0) {
1083 layer->len = cblk->passes[n - 1].rate;
1084 layer->data = cblk->data;
1085 layer->disto = cblk->passes[n - 1].distortiondec;
1087 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1088 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1089 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1092 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1095 cblk->numpassesinlayers = n;
1103 void tcd_makelayer_v2( opj_tcd_v2_t *tcd,
1108 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1111 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1113 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
1115 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1116 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1118 for (resno = 0; resno < tilec->numresolutions; resno++) {
1119 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1121 for (bandno = 0; bandno < res->numbands; bandno++) {
1122 opj_tcd_band_v2_t *band = &res->bands[bandno];
1124 for (precno = 0; precno < res->pw * res->ph; precno++) {
1125 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1127 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1128 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1129 opj_tcd_layer_t *layer = &cblk->layers[layno];
1133 cblk->numpassesinlayers = 0;
1136 n = cblk->numpassesinlayers;
1138 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1141 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1145 dd = pass->distortiondec;
1147 dr = pass->rate - cblk->passes[n - 1].rate;
1148 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1156 if (dd / dr >= thresh)
1160 layer->numpasses = n - cblk->numpassesinlayers;
1162 if (!layer->numpasses) {
1167 if (cblk->numpassesinlayers == 0) {
1168 layer->len = cblk->passes[n - 1].rate;
1169 layer->data = cblk->data;
1170 layer->disto = cblk->passes[n - 1].distortiondec;
1172 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1173 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1174 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1177 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1180 cblk->numpassesinlayers = n;
1188 void tcd_makelayer_fixed_v2(opj_tcd_v2_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
1189 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1190 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
1191 OPJ_INT32 matrice[10][10][3];
1194 opj_cp_v2_t *cp = tcd->cp;
1195 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1196 opj_tcp_v2_t *tcd_tcp = tcd->tcp;
1198 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1199 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1201 for (i = 0; i < tcd_tcp->numlayers; i++) {
1202 for (j = 0; j < tilec->numresolutions; j++) {
1203 for (k = 0; k < 3; k++) {
1205 (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
1206 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
1211 for (resno = 0; resno < tilec->numresolutions; resno++) {
1212 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1214 for (bandno = 0; bandno < res->numbands; bandno++) {
1215 opj_tcd_band_v2_t *band = &res->bands[bandno];
1217 for (precno = 0; precno < res->pw * res->ph; precno++) {
1218 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1220 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1221 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1222 opj_tcd_layer_t *layer = &cblk->layers[layno];
1224 OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
1226 /* Correction of the matrix of coefficient to include the IMSB information */
1228 value = matrice[layno][resno][bandno];
1229 if (imsb >= value) {
1235 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
1236 if (imsb >= matrice[layno - 1][resno][bandno]) {
1237 value -= (imsb - matrice[layno - 1][resno][bandno]);
1245 cblk->numpassesinlayers = 0;
1248 n = cblk->numpassesinlayers;
1249 if (cblk->numpassesinlayers == 0) {
1251 n = 3 * value - 2 + cblk->numpassesinlayers;
1253 n = cblk->numpassesinlayers;
1256 n = 3 * value + cblk->numpassesinlayers;
1259 layer->numpasses = n - cblk->numpassesinlayers;
1261 if (!layer->numpasses)
1264 if (cblk->numpassesinlayers == 0) {
1265 layer->len = cblk->passes[n - 1].rate;
1266 layer->data = cblk->data;
1268 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1269 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1273 cblk->numpassesinlayers = n;
1281 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1282 int compno, resno, bandno, precno, cblkno, passno, layno;
1284 double cumdisto[100]; /* fixed_quality */
1285 const double K = 1; /* 1.1; fixed_quality */
1288 opj_cp_t *cp = tcd->cp;
1289 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1290 opj_tcp_t *tcd_tcp = tcd->tcp;
1295 tcd_tile->numpix = 0; /* fixed_quality */
1297 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1298 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1301 for (resno = 0; resno < tilec->numresolutions; resno++) {
1302 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1304 for (bandno = 0; bandno < res->numbands; bandno++) {
1305 opj_tcd_band_t *band = &res->bands[bandno];
1307 for (precno = 0; precno < res->pw * res->ph; precno++) {
1308 opj_tcd_precinct_t *prc = &band->precincts[precno];
1310 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1311 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1313 for (passno = 0; passno < cblk->totalpasses; passno++) {
1314 opj_tcd_pass_t *pass = &cblk->passes[passno];
1319 dd = pass->distortiondec;
1321 dr = pass->rate - cblk->passes[passno - 1].rate;
1322 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1328 if (rdslope < min) {
1331 if (rdslope > max) {
1337 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1338 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1344 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1345 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1346 * ((double)(tilec->numpix));
1351 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1352 tile_info->numpix = tcd_tile->numpix;
1353 tile_info->distotile = tcd_tile->distotile;
1354 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1357 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1361 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1362 double goodthresh = 0;
1363 double stable_thresh = 0;
1365 double distotarget; /* fixed_quality */
1368 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1370 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1371 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1372 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1373 ==> possible to have some lossy layers and the last layer for sure lossless */
1374 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1375 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1378 for (i = 0; i < 128; i++) {
1380 double distoachieved = 0; /* fixed_quality */
1381 thresh = (lo + hi) / 2;
1383 tcd_makelayer(tcd, layno, thresh, 0);
1385 if (cp->fixed_quality) { /* fixed_quality */
1387 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);
1392 distoachieved = layno == 0 ?
1393 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1394 if (distoachieved < distotarget) {
1396 stable_thresh = thresh;
1403 distoachieved = (layno == 0) ?
1404 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1405 if (distoachieved < distotarget) {
1407 stable_thresh = thresh;
1413 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);
1414 /* TODO: what to do with l ??? seek / tell ??? */
1415 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1421 stable_thresh = thresh;
1425 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1436 if(cstr_info) { /* Threshold for Marcela Index */
1437 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1439 tcd_makelayer(tcd, layno, goodthresh, 1);
1442 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1448 opj_bool tcd_rateallocate_v2( opj_tcd_v2_t *tcd,
1450 OPJ_UINT32 * p_data_written,
1452 opj_codestream_info_t *cstr_info)
1454 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
1456 OPJ_FLOAT64 min, max;
1457 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
1458 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
1459 OPJ_FLOAT64 maxSE = 0;
1461 opj_cp_v2_t *cp = tcd->cp;
1462 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1463 opj_tcp_v2_t *tcd_tcp = tcd->tcp;
1468 tcd_tile->numpix = 0; /* fixed_quality */
1470 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1471 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1474 for (resno = 0; resno < tilec->numresolutions; resno++) {
1475 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1477 for (bandno = 0; bandno < res->numbands; bandno++) {
1478 opj_tcd_band_v2_t *band = &res->bands[bandno];
1480 for (precno = 0; precno < res->pw * res->ph; precno++) {
1481 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1483 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1484 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1486 for (passno = 0; passno < cblk->totalpasses; passno++) {
1487 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1489 OPJ_FLOAT64 dd, rdslope;
1493 dd = pass->distortiondec;
1495 dr = pass->rate - cblk->passes[passno - 1].rate;
1496 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1504 if (rdslope < min) {
1508 if (rdslope > max) {
1514 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1515 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1521 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
1522 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
1523 * ((OPJ_FLOAT64)(tilec->numpix));
1528 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1529 tile_info->numpix = tcd_tile->numpix;
1530 tile_info->distotile = tcd_tile->distotile;
1531 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
1534 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1535 OPJ_FLOAT64 lo = min;
1536 OPJ_FLOAT64 hi = max;
1537 opj_bool success = OPJ_FALSE;
1538 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
1539 OPJ_FLOAT64 goodthresh = 0;
1540 OPJ_FLOAT64 stable_thresh = 0;
1542 OPJ_FLOAT64 distotarget; /* fixed_quality */
1545 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
1547 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1548 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1549 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1550 ==> possible to have some lossy layers and the last layer for sure lossless */
1551 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))) {
1552 opj_t2_v2_t*t2 = t2_create_v2(tcd->image, cp);
1553 OPJ_FLOAT64 thresh = 0;
1559 for (i = 0; i < 128; ++i) {
1560 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
1562 thresh = (lo + hi) / 2;
1564 tcd_makelayer_v2(tcd, layno, thresh, 0);
1566 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
1567 if(cp->m_specific_param.m_enc.m_cinema){
1568 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)) {
1574 distoachieved = layno == 0 ?
1575 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1577 if (distoachieved < distotarget) {
1579 stable_thresh = thresh;
1586 distoachieved = (layno == 0) ?
1587 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1589 if (distoachieved < distotarget) {
1591 stable_thresh = thresh;
1597 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))
1599 /* TODO: what to do with l ??? seek / tell ??? */
1600 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1606 stable_thresh = thresh;
1611 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1623 if(cstr_info) { /* Threshold for Marcela Index */
1624 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1627 tcd_makelayer_v2(tcd, layno, goodthresh, 1);
1630 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1637 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1639 int l, i, numpacks = 0;
1640 opj_tcd_tile_t *tile = NULL;
1641 opj_tcp_t *tcd_tcp = NULL;
1642 opj_cp_t *cp = NULL;
1644 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1645 opj_tccp_t *tccp = &tcp->tccps[0];
1646 opj_image_t *image = tcd->image;
1648 opj_t1_t *t1 = NULL; /* T1 component */
1649 opj_t2_t *t2 = NULL; /* T2 component */
1651 tcd->tcd_tileno = tileno;
1652 tcd->tcd_tile = tcd->tcd_image->tiles;
1653 tcd->tcp = &tcd->cp->tcps[tileno];
1655 tile = tcd->tcd_tile;
1659 if(tcd->cur_tp_num == 0){
1660 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1661 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1663 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1664 for (i = 0; i < tilec_idx->numresolutions; i++) {
1665 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1667 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1668 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1670 numpacks += res_idx->pw * res_idx->ph;
1672 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1673 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1675 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1679 /*---------------TILE-------------------*/
1681 for (compno = 0; compno < tile->numcomps; compno++) {
1684 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1685 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1686 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1688 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1689 int tw = tilec->x1 - tilec->x0;
1690 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1692 /* extract tile data */
1694 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1695 for (y = tilec->y0; y < tilec->y1; y++) {
1696 /* start of the src tile scanline */
1697 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1698 /* start of the dst tile scanline */
1699 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1700 for (x = tilec->x0; x < tilec->x1; x++) {
1701 *tile_data++ = *data++ - adjust;
1704 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1705 for (y = tilec->y0; y < tilec->y1; y++) {
1706 /* start of the src tile scanline */
1707 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1708 /* start of the dst tile scanline */
1709 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1710 for (x = tilec->x0; x < tilec->x1; x++) {
1711 *tile_data++ = (*data++ - adjust) << 11;
1718 /*----------------MCT-------------------*/
1720 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1721 if (tcd_tcp->tccps[0].qmfbid == 0) {
1722 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1724 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1728 /*----------------DWT---------------------*/
1730 for (compno = 0; compno < tile->numcomps; compno++) {
1731 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1732 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1734 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1735 dwt_encode_real(tilec);
1739 /*------------------TIER1-----------------*/
1740 t1 = t1_create(tcd->cinfo);
1741 t1_encode_cblks(t1, tile, tcd_tcp);
1744 /*-----------RATE-ALLOCATE------------------*/
1748 cstr_info->index_write = 0;
1750 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1751 /* Normal Rate/distortion allocation */
1752 tcd_rateallocate(tcd, dest, len, cstr_info);
1754 /* Fixed layer allocation */
1755 tcd_rateallocate_fixed(tcd);
1758 /*--------------TIER2------------------*/
1762 cstr_info->index_write = 1;
1765 t2 = t2_create(tcd->cinfo, image, cp);
1766 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);
1769 /*---------------CLEAN-------------------*/
1772 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1773 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1774 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1776 /* cleaning memory */
1777 for (compno = 0; compno < tile->numcomps; compno++) {
1778 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1779 opj_aligned_free(tilec->data);
1786 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1790 double tile_time, t1_time, dwt_time;
1791 opj_tcd_tile_t *tile = NULL;
1793 opj_t1_t *t1 = NULL; /* T1 component */
1794 opj_t2_t *t2 = NULL; /* T2 component */
1796 tcd->tcd_tileno = tileno;
1797 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1798 tcd->tcp = &(tcd->cp->tcps[tileno]);
1799 tile = tcd->tcd_tile;
1801 tile_time = opj_clock(); /* time needed to decode a tile */
1802 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1806 int resno, compno, numprec = 0;
1807 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1808 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1809 opj_tccp_t *tccp = &tcp->tccps[compno];
1810 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1811 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1812 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1813 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1814 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1815 numprec += res_idx->pw * res_idx->ph;
1816 if (tccp->csty & J2K_CP_CSTY_PRT) {
1817 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1818 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1821 cstr_info->tile[tileno].pdx[resno] = 15;
1822 cstr_info->tile[tileno].pdy[resno] = 15;
1826 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1827 cstr_info->packno = 0;
1831 /*--------------TIER2------------------*/
1833 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1834 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1839 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1842 /*------------------TIER1-----------------*/
1844 t1_time = opj_clock(); /* time needed to decode a tile */
1845 t1 = t1_create(tcd->cinfo);
1846 for (compno = 0; compno < tile->numcomps; ++compno) {
1847 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1848 /* The +3 is headroom required by the vectorized DWT */
1849 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1850 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1853 t1_time = opj_clock() - t1_time;
1854 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1856 /*----------------DWT---------------------*/
1858 dwt_time = opj_clock(); /* time needed to decode a tile */
1859 for (compno = 0; compno < tile->numcomps; compno++) {
1860 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1863 if (tcd->cp->reduce != 0) {
1864 tcd->image->comps[compno].resno_decoded =
1865 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1866 if (tcd->image->comps[compno].resno_decoded < 0) {
1867 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1868 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1873 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1874 if(numres2decode > 0){
1875 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1876 dwt_decode(tilec, numres2decode);
1878 dwt_decode_real(tilec, numres2decode);
1882 dwt_time = opj_clock() - dwt_time;
1883 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1885 /*----------------MCT-------------------*/
1887 if (tcd->tcp->mct) {
1888 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1890 if (tile->numcomps >= 3 ){
1891 if (tcd->tcp->tccps[0].qmfbid == 1) {
1893 tile->comps[0].data,
1894 tile->comps[1].data,
1895 tile->comps[2].data,
1899 (float*)tile->comps[0].data,
1900 (float*)tile->comps[1].data,
1901 (float*)tile->comps[2].data,
1905 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1909 /*---------------TILE-------------------*/
1911 for (compno = 0; compno < tile->numcomps; ++compno) {
1912 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1913 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1914 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1915 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1916 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1917 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1919 int tw = tilec->x1 - tilec->x0;
1922 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1923 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1927 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1929 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1930 for(j = res->y0; j < res->y1; ++j) {
1931 for(i = res->x0; i < res->x1; ++i) {
1932 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1934 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1938 for(j = res->y0; j < res->y1; ++j) {
1939 for(i = res->x0; i < res->x1; ++i) {
1940 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1941 int v = lrintf(tmp);
1943 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1947 opj_aligned_free(tilec->data);
1950 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1951 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1960 void tcd_free_decode(opj_tcd_t *tcd) {
1961 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1962 opj_free(tcd_image->tiles);
1965 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1966 int compno,resno,bandno,precno;
1968 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1970 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1971 for (compno = 0; compno < tile->numcomps; compno++) {
1972 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1973 for (resno = 0; resno < tilec->numresolutions; resno++) {
1974 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1975 for (bandno = 0; bandno < res->numbands; bandno++) {
1976 opj_tcd_band_t *band = &res->bands[bandno];
1977 for (precno = 0; precno < res->ph * res->pw; precno++) {
1978 opj_tcd_precinct_t *prec = &band->precincts[precno];
1979 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1980 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1982 opj_free(band->precincts);
1985 opj_free(tilec->resolutions);
1987 opj_free(tile->comps);
1991 opj_bool tcd_init_v2( opj_tcd_v2_t *p_tcd,
1992 opj_image_t * p_image,
1993 opj_cp_v2_t * p_cp )
1995 OPJ_UINT32 l_tile_comp_size;
1997 p_tcd->image = p_image;
2000 p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t));
2001 if (! p_tcd->tcd_image->tiles) {
2004 memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t));
2006 l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t);
2007 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size);
2008 if (! p_tcd->tcd_image->tiles->comps ) {
2011 memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
2013 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
2014 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
2020 Destroy a previously created TCD handle
2022 void tcd_destroy_v2(opj_tcd_v2_t *tcd) {
2026 if (tcd->tcd_image) {
2027 opj_free(tcd->tcd_image);
2028 tcd->tcd_image = 00;
2034 /* ----------------------------------------------------------------------- */
2036 * Initialize the tile coder and may reuse some meory.
2037 * @param p_tcd TCD handle.
2038 * @param p_image raw image.
2039 * @param p_cp coding parameters.
2040 * @param p_tile_no current tile index to encode.
2042 * @return true if the encoding values could be set (false otherwise).
2044 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
2045 opj_bool FUNCTION ( opj_tcd_v2_t *p_tcd, \
2046 OPJ_UINT32 p_tile_no \
2049 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
2050 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
2051 opj_tcp_v2_t * l_tcp = 00; \
2052 opj_cp_v2_t * l_cp = 00; \
2053 opj_tcd_tile_v2_t * l_tile = 00; \
2054 opj_tccp_t *l_tccp = 00; \
2055 opj_tcd_tilecomp_v2_t *l_tilec = 00; \
2056 opj_image_comp_t * l_image_comp = 00; \
2057 opj_tcd_resolution_v2_t *l_res = 00; \
2058 opj_tcd_band_v2_t *l_band = 00; \
2059 opj_stepsize_t * l_step_size = 00; \
2060 opj_tcd_precinct_v2_t *l_current_precinct = 00; \
2061 TYPE* l_code_block = 00; \
2062 opj_image_t *l_image = 00; \
2064 OPJ_UINT32 l_level_no; \
2065 OPJ_UINT32 l_pdx, l_pdy; \
2066 OPJ_UINT32 l_gain; \
2067 OPJ_INT32 l_x0b, l_y0b; \
2068 /* extent of precincts , top left, bottom right**/ \
2069 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
2070 /* number of precinct for a resolution */ \
2071 OPJ_UINT32 l_nb_precincts; \
2072 /* room needed to store l_nb_precinct precinct for a resolution */ \
2073 OPJ_UINT32 l_nb_precinct_size; \
2074 /* number of code blocks for a precinct*/ \
2075 OPJ_UINT32 l_nb_code_blocks; \
2076 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
2077 OPJ_UINT32 l_nb_code_blocks_size; \
2078 /* size of data for a tile */ \
2079 OPJ_UINT32 l_data_size; \
2082 l_tcp = &(l_cp->tcps[p_tile_no]); \
2083 l_tile = p_tcd->tcd_image->tiles; \
2084 l_tccp = l_tcp->tccps; \
2085 l_tilec = l_tile->comps; \
2086 l_image = p_tcd->image; \
2087 l_image_comp = p_tcd->image->comps; \
2089 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
2090 q = p_tile_no / l_cp->tw; \
2091 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
2093 /* 4 borders of the tile rescale on the image if necessary */ \
2094 l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \
2095 l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \
2096 l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \
2097 l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \
2098 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/\
2100 /*tile->numcomps = image->numcomps; */ \
2101 for(compno = 0; compno < l_tile->numcomps; ++compno) { \
2102 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
2104 /* border of each l_tile component (global) */ \
2105 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx); \
2106 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy); \
2107 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx); \
2108 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy); \
2109 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/\
2111 l_data_size = (l_tilec->x1 - l_tilec->x0) \
2112 * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 ); \
2113 l_tilec->numresolutions = l_tccp->numresolutions; \
2114 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
2115 l_tilec->minimum_num_resolutions = 1; \
2118 l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
2119 - l_cp->m_specific_param.m_dec.m_reduce; \
2122 if (l_tilec->data == 00) { \
2123 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
2124 if (! l_tilec->data ) { \
2127 /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
2129 l_tilec->data_size = l_data_size; \
2131 else if (l_data_size > l_tilec->data_size) { \
2132 l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
2133 if (! l_tilec->data) { \
2136 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
2137 l_tilec->data_size = l_data_size; \
2140 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t); \
2142 if (l_tilec->resolutions == 00) { \
2143 l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \
2144 if (! l_tilec->resolutions ) { \
2147 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/ \
2148 l_tilec->resolutions_size = l_data_size; \
2149 memset(l_tilec->resolutions,0,l_data_size); \
2151 else if (l_data_size > l_tilec->resolutions_size) { \
2152 l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
2153 if (! l_tilec->resolutions) { \
2156 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
2157 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
2158 l_tilec->resolutions_size = l_data_size; \
2161 l_level_no = l_tilec->numresolutions - 1; \
2162 l_res = l_tilec->resolutions; \
2163 l_step_size = l_tccp->stepsizes; \
2164 if (l_tccp->qmfbid == 0) { \
2165 l_gain_ptr = &dwt_getgain_real_v2; \
2168 l_gain_ptr = &dwt_getgain_v2; \
2170 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
2172 for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
2173 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
2174 OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; \
2175 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
2176 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
2178 /* border for each resolution level (global) */ \
2179 l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
2180 l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
2181 l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
2182 l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
2183 /*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);*/ \
2184 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
2185 l_pdx = l_tccp->prcw[resno]; \
2186 l_pdy = l_tccp->prch[resno]; \
2187 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
2188 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
2189 l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \
2190 l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \
2191 l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \
2192 l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \
2193 /*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 );*/ \
2195 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
2196 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
2197 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
2199 l_nb_precincts = l_res->pw * l_res->ph; \
2200 l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t); \
2202 tlcbgxstart = l_tl_prc_x_start; \
2203 tlcbgystart = l_tl_prc_y_start; \
2204 brcbgxend = l_br_prc_x_end; \
2205 brcbgyend = l_br_prc_y_end; \
2206 cbgwidthexpn = l_pdx; \
2207 cbgheightexpn = l_pdy; \
2208 l_res->numbands = 1; \
2211 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1); \
2212 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1); \
2213 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1); \
2214 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1); \
2215 cbgwidthexpn = l_pdx - 1; \
2216 cbgheightexpn = l_pdy - 1; \
2217 l_res->numbands = 3; \
2220 cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn); \
2221 cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn); \
2222 l_band = l_res->bands; \
2224 for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
2226 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
2229 l_band->bandno = 0 ; \
2230 l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
2231 l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
2232 l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
2233 l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
2236 l_band->bandno = bandno + 1; \
2237 /* x0b = 1 if bandno = 1 or 3 */ \
2238 l_x0b = l_band->bandno&1; \
2239 /* y0b = 1 if bandno = 2 or 3 */ \
2240 l_y0b = (l_band->bandno)>>1; \
2241 /* l_band border (global) */ \
2242 l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
2243 l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
2244 l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
2245 l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
2248 /** avoid an if with storing function pointer */ \
2249 l_gain = (*l_gain_ptr) (l_band->bandno); \
2250 numbps = l_image_comp->prec + l_gain; \
2251 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\
2252 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */\
2254 if (! l_band->precincts) { \
2255 l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size);\
2256 if (! l_band->precincts) { \
2259 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size); */ \
2260 memset(l_band->precincts,0,l_nb_precinct_size); \
2261 l_band->precincts_data_size = l_nb_precinct_size; \
2263 else if (l_band->precincts_data_size < l_nb_precinct_size) { \
2265 l_band->precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
2266 if (! l_band->precincts) { \
2269 /*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);*/\
2270 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\
2271 l_band->precincts_data_size = l_nb_precinct_size; \
2274 l_current_precinct = l_band->precincts; \
2275 for (precno = 0; precno < l_nb_precincts; ++precno) { \
2276 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
2277 OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
2278 OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn); \
2279 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
2280 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
2281 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/\
2282 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/\
2284 /* precinct size (global) */ \
2285 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
2287 l_current_precinct->x0 = int_max(cbgxstart, l_band->x0); \
2288 l_current_precinct->y0 = int_max(cbgystart, l_band->y0); \
2289 l_current_precinct->x1 = int_min(cbgxend, l_band->x1); \
2290 l_current_precinct->y1 = int_min(cbgyend, l_band->y1); \
2291 /*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);*/ \
2293 tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn; \
2294 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
2295 tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn; \
2296 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
2297 brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn; \
2298 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
2299 brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
2300 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
2301 l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; \
2302 l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
2304 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
2305 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
2306 l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE); \
2308 if (! l_current_precinct->cblks.ELEMENT) { \
2309 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
2310 if (! l_current_precinct->cblks.ELEMENT ) { \
2313 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/ \
2315 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
2317 l_current_precinct->block_size = l_nb_code_blocks_size; \
2319 else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
2320 l_current_precinct->cblks.ELEMENT = (TYPE*) \
2321 opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
2322 if (! l_current_precinct->cblks.ELEMENT ) { \
2325 /*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); */ \
2327 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
2329 ,l_nb_code_blocks_size - l_current_precinct->block_size); \
2331 l_current_precinct->block_size = l_nb_code_blocks_size; \
2334 if (! l_current_precinct->incltree) { \
2335 l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw, \
2336 l_current_precinct->ch); \
2339 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree, \
2340 l_current_precinct->cw, \
2341 l_current_precinct->ch); \
2344 if (! l_current_precinct->incltree) { \
2345 fprintf(stderr, "WARNING: No incltree created.\n");\
2346 /*return OPJ_FALSE;*/ \
2349 if (! l_current_precinct->imsbtree) { \
2350 l_current_precinct->imsbtree = tgt_create_v2( \
2351 l_current_precinct->cw, \
2352 l_current_precinct->ch);\
2355 l_current_precinct->imsbtree = tgt_init( \
2356 l_current_precinct->imsbtree, \
2357 l_current_precinct->cw, \
2358 l_current_precinct->ch); \
2361 if (! l_current_precinct->imsbtree) { \
2362 fprintf(stderr, "WARNING: No imsbtree created.\n");\
2363 /*return OPJ_FALSE;*/ \
2366 l_code_block = l_current_precinct->cblks.ELEMENT; \
2368 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
2369 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
2370 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
2371 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
2372 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);\
2374 /* code-block size (global) */ \
2375 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \
2376 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \
2377 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1); \
2378 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1); \
2380 if (! FUNCTION_ELEMENT(l_code_block)) { \
2385 ++l_current_precinct; \
2401 MACRO_TCD_ALLOCATE(tcd_init_encode_tile, opj_tcd_cblk_enc_v2_t, 1.f, enc, tcd_code_block_enc_allocate)
2402 MACRO_TCD_ALLOCATE(tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, tcd_code_block_dec_allocate)
2404 #undef MACRO_TCD_ALLOCATE
2407 * Allocates memory for an encoding code block.
2409 opj_bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block)
2411 if (! p_code_block->data) {
2413 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
2414 if(! p_code_block->data) {
2418 p_code_block->data+=1;
2420 /* no memset since data */
2421 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
2422 if (! p_code_block->layers) {
2426 p_code_block->passes = (opj_tcd_pass_v2_t*) opj_malloc(100 * sizeof(opj_tcd_pass_v2_t));
2427 if (! p_code_block->passes) {
2432 memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
2433 memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_v2_t));
2439 * Allocates memory for a decoding code block.
2441 opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block)
2443 OPJ_UINT32 l_seg_size;
2445 if (! p_code_block->data) {
2447 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
2448 if (! p_code_block->data) {
2451 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
2453 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
2454 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
2455 if (! p_code_block->segs) {
2458 memset(p_code_block->segs,0,l_seg_size);
2459 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
2461 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
2462 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
2465 /*p_code_block->numsegs = 0; */
2470 OPJ_UINT32 tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd )
2473 OPJ_UINT32 l_data_size = 0;
2474 opj_image_comp_t * l_img_comp = 00;
2475 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
2476 opj_tcd_resolution_v2_t * l_res = 00;
2477 OPJ_UINT32 l_size_comp, l_remaining;
2479 l_tile_comp = p_tcd->tcd_image->tiles->comps;
2480 l_img_comp = p_tcd->image->comps;
2482 for (i=0;i<p_tcd->image->numcomps;++i) {
2483 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2484 l_remaining = l_img_comp->prec & 7; /* (%8) */
2490 if (l_size_comp == 3) {
2494 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
2495 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
2503 opj_bool tcd_encode_tile_v2(opj_tcd_v2_t *p_tcd,
2504 OPJ_UINT32 p_tile_no,
2506 OPJ_UINT32 * p_data_written,
2507 OPJ_UINT32 p_max_length,
2508 opj_codestream_info_t *p_cstr_info)
2511 if (p_tcd->cur_tp_num == 0) {
2513 p_tcd->tcd_tileno = p_tile_no;
2514 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
2516 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
2518 OPJ_UINT32 l_num_packs = 0;
2520 opj_tcd_tilecomp_v2_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
2521 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
2523 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
2524 opj_tcd_resolution_v2_t *l_res_idx = &l_tilec_idx->resolutions[i];
2526 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
2527 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
2529 l_num_packs += l_res_idx->pw * l_res_idx->ph;
2530 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
2531 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
2533 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));
2537 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
2538 /*---------------TILE-------------------*/
2539 if (! tcd_dc_level_shift_encode(p_tcd)) {
2542 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
2544 /* FIXME _ProfStart(PGROUP_MCT); */
2545 if (! tcd_mct_encode(p_tcd)) {
2548 /* FIXME _ProfStop(PGROUP_MCT); */
2550 /* FIXME _ProfStart(PGROUP_DWT); */
2551 if (! tcd_dwt_encode(p_tcd)) {
2554 /* FIXME _ProfStop(PGROUP_DWT); */
2556 /* FIXME _ProfStart(PGROUP_T1); */
2557 if (! tcd_t1_encode(p_tcd)) {
2560 /* FIXME _ProfStop(PGROUP_T1); */
2562 /* FIXME _ProfStart(PGROUP_RATE); */
2563 if (! tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
2566 /* FIXME _ProfStop(PGROUP_RATE); */
2569 /*--------------TIER2------------------*/
2573 p_cstr_info->index_write = 1;
2575 /* FIXME _ProfStart(PGROUP_T2); */
2577 if (! tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
2580 /* FIXME _ProfStop(PGROUP_T2); */
2582 /*---------------CLEAN-------------------*/
2587 opj_bool tcd_decode_tile_v2(
2588 opj_tcd_v2_t *p_tcd,
2590 OPJ_UINT32 p_max_length,
2591 OPJ_UINT32 p_tile_no,
2592 opj_codestream_index_t *p_cstr_index)
2594 OPJ_UINT32 l_data_read;
2595 p_tcd->tcd_tileno = p_tile_no;
2596 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
2598 #ifdef TODO_MSD /* FIXME */
2601 OPJ_UINT32 resno, compno, numprec = 0;
2602 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
2603 opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0];
2604 opj_tccp_t *tccp = &tcp->tccps[compno];
2605 opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
2606 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
2607 opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno];
2608 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
2609 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
2610 numprec += res_idx->pw * res_idx->ph;
2611 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
2612 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
2615 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
2616 p_cstr_info->packno = 0;
2621 /*--------------TIER2------------------*/
2622 /* FIXME _ProfStart(PGROUP_T2); */
2625 (! tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
2629 /* FIXME _ProfStop(PGROUP_T2); */
2631 /*------------------TIER1-----------------*/
2633 /* FIXME _ProfStart(PGROUP_T1); */
2635 (! tcd_t1_decode(p_tcd))
2639 /* FIXME _ProfStop(PGROUP_T1); */
2641 /*----------------DWT---------------------*/
2643 /* FIXME _ProfStart(PGROUP_DWT); */
2645 (! tcd_dwt_decode(p_tcd))
2649 /* FIXME _ProfStop(PGROUP_DWT); */
2651 /*----------------MCT-------------------*/
2652 /* FIXME _ProfStart(PGROUP_MCT); */
2654 (! tcd_mct_decode(p_tcd))
2658 /* FIXME _ProfStop(PGROUP_MCT); */
2660 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
2662 (! tcd_dc_level_shift_decode(p_tcd))
2666 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
2669 /*---------------TILE-------------------*/
2673 opj_bool tcd_update_tile_data (
2674 opj_tcd_v2_t *p_tcd,
2676 OPJ_UINT32 p_dest_length
2679 OPJ_UINT32 i,j,k,l_data_size = 0;
2680 opj_image_comp_t * l_img_comp = 00;
2681 opj_tcd_tilecomp_v2_t * l_tilec = 00;
2682 opj_tcd_resolution_v2_t * l_res;
2683 OPJ_UINT32 l_size_comp, l_remaining;
2684 OPJ_UINT32 l_stride, l_width,l_height;
2686 l_data_size = tcd_get_decoded_tile_size(p_tcd);
2687 if (l_data_size > p_dest_length) {
2691 l_tilec = p_tcd->tcd_image->tiles->comps;
2692 l_img_comp = p_tcd->image->comps;
2694 for (i=0;i<p_tcd->image->numcomps;++i) {
2695 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2696 l_remaining = l_img_comp->prec & 7; /* (%8) */
2697 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
2698 l_width = (l_res->x1 - l_res->x0);
2699 l_height = (l_res->y1 - l_res->y0);
2700 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
2706 if (l_size_comp == 3) {
2710 switch (l_size_comp)
2714 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
2715 const OPJ_INT32 * l_src_ptr = l_tilec->data;
2717 if (l_img_comp->sgnd) {
2718 for (j=0;j<l_height;++j) {
2719 for (k=0;k<l_width;++k) {
2720 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
2722 l_src_ptr += l_stride;
2726 for (j=0;j<l_height;++j) {
2727 for (k=0;k<l_width;++k) {
2728 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
2730 l_src_ptr += l_stride;
2734 p_dest = (OPJ_BYTE *)l_dest_ptr;
2739 const OPJ_INT32 * l_src_ptr = l_tilec->data;
2740 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
2742 if (l_img_comp->sgnd) {
2743 for (j=0;j<l_height;++j) {
2744 for (k=0;k<l_width;++k) {
2745 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
2747 l_src_ptr += l_stride;
2751 for (j=0;j<l_height;++j) {
2752 for (k=0;k<l_width;++k) {
2753 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
2755 l_src_ptr += l_stride;
2759 p_dest = (OPJ_BYTE*) l_dest_ptr;
2764 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
2765 OPJ_INT32 * l_src_ptr = l_tilec->data;
2767 for (j=0;j<l_height;++j) {
2768 for (k=0;k<l_width;++k) {
2769 *(l_dest_ptr++) = (*(l_src_ptr++));
2771 l_src_ptr += l_stride;
2774 p_dest = (OPJ_BYTE*) l_dest_ptr;
2789 void tcd_free_tile(opj_tcd_v2_t *p_tcd)
2791 OPJ_UINT32 compno, resno, bandno, precno;
2792 opj_tcd_tile_v2_t *l_tile = 00;
2793 opj_tcd_tilecomp_v2_t *l_tile_comp = 00;
2794 opj_tcd_resolution_v2_t *l_res = 00;
2795 opj_tcd_band_v2_t *l_band = 00;
2796 opj_tcd_precinct_v2_t *l_precinct = 00;
2797 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
2798 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00;
2804 if (! p_tcd->tcd_image) {
2808 if (p_tcd->m_is_decoder) {
2809 l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate;
2812 /* FIXME l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate; */
2815 l_tile = p_tcd->tcd_image->tiles;
2820 l_tile_comp = l_tile->comps;
2822 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2823 l_res = l_tile_comp->resolutions;
2826 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t);
2827 for (resno = 0; resno < l_nb_resolutions; ++resno) {
2828 l_band = l_res->bands;
2829 for (bandno = 0; bandno < 3; ++bandno) {
2830 l_precinct = l_band->precincts;
2833 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t);
2834 for (precno = 0; precno < l_nb_precincts; ++precno) {
2835 tgt_destroy(l_precinct->incltree);
2836 l_precinct->incltree = 00;
2837 tgt_destroy(l_precinct->imsbtree);
2838 l_precinct->imsbtree = 00;
2839 (*l_tcd_code_block_deallocate) (l_precinct);
2843 opj_free(l_band->precincts);
2844 l_band->precincts = 00;
2851 opj_free(l_tile_comp->resolutions);
2852 l_tile_comp->resolutions = 00;
2855 if (l_tile_comp->data) {
2856 opj_free(l_tile_comp->data);
2857 l_tile_comp->data = 00;
2862 opj_free(l_tile->comps);
2864 opj_free(p_tcd->tcd_image->tiles);
2865 p_tcd->tcd_image->tiles = 00;
2869 opj_bool tcd_t2_decode (
2870 opj_tcd_v2_t *p_tcd,
2871 OPJ_BYTE * p_src_data,
2872 OPJ_UINT32 * p_data_read,
2873 OPJ_UINT32 p_max_src_size,
2874 opj_codestream_index_t *p_cstr_index
2879 l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
2884 if (! t2_decode_packets_v2(
2887 p_tcd->tcd_image->tiles,
2892 t2_destroy_v2(l_t2);
2896 t2_destroy_v2(l_t2);
2898 /*---------------CLEAN-------------------*/
2902 opj_bool tcd_t1_decode ( opj_tcd_v2_t *p_tcd )
2906 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2907 opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps;
2908 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2911 l_t1 = t1_create_v2();
2916 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2917 /* The +3 is headroom required by the vectorized DWT */
2918 t1_decode_cblks_v2(l_t1, l_tile_comp, l_tccp);
2923 t1_destroy_v2(l_t1);
2929 opj_bool tcd_dwt_decode ( opj_tcd_v2_t *p_tcd )
2932 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2933 opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2934 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2935 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2937 for (compno = 0; compno < l_tile->numcomps; compno++) {
2939 if (tcd->cp->reduce != 0) {
2940 tcd->image->comps[compno].resno_decoded =
2941 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
2942 if (tcd->image->comps[compno].resno_decoded < 0)
2947 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
2948 if(numres2decode > 0){
2951 if (l_tccp->qmfbid == 1) {
2952 if (! dwt_decode_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2957 if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2969 opj_bool tcd_mct_decode ( opj_tcd_v2_t *p_tcd )
2971 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2972 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
2973 opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2974 OPJ_UINT32 l_samples,i;
2980 l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
2982 if (l_tile->numcomps >= 3 ){
2983 if (l_tcp->mct == 2) {
2986 if (! l_tcp->m_mct_decoding_matrix) {
2990 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
2995 for (i=0;i<l_tile->numcomps;++i) {
2996 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
3000 if (! mct_decode_custom(/* MCT data */
3001 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
3002 /* size of components */
3006 /* nb of components (i.e. size of pData) */
3008 /* tells if the data is signed */
3009 p_tcd->image->comps->sgnd)) {
3017 if (l_tcp->tccps->qmfbid == 1) {
3018 mct_decode( l_tile->comps[0].data,
3019 l_tile->comps[1].data,
3020 l_tile->comps[2].data,
3024 mct_decode_real( (float*)l_tile->comps[0].data,
3025 (float*)l_tile->comps[1].data,
3026 (float*)l_tile->comps[2].data,
3032 /* FIXME need to use opj_event_msg_v2 function */
3033 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
3040 opj_bool tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd )
3043 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
3044 opj_tccp_t * l_tccp = 00;
3045 opj_image_comp_t * l_img_comp = 00;
3046 opj_tcd_resolution_v2_t* l_res = 00;
3047 opj_tcp_v2_t * l_tcp = 00;
3048 opj_tcd_tile_v2_t * l_tile;
3049 OPJ_UINT32 l_width,l_height,i,j;
3050 OPJ_INT32 * l_current_ptr;
3051 OPJ_INT32 l_min, l_max;
3052 OPJ_UINT32 l_stride;
3054 l_tile = p_tcd->tcd_image->tiles;
3055 l_tile_comp = l_tile->comps;
3057 l_tccp = p_tcd->tcp->tccps;
3058 l_img_comp = p_tcd->image->comps;
3060 for (compno = 0; compno < l_tile->numcomps; compno++) {
3061 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
3062 l_width = (l_res->x1 - l_res->x0);
3063 l_height = (l_res->y1 - l_res->y0);
3064 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
3066 if (l_img_comp->sgnd) {
3067 l_min = -(1 << (l_img_comp->prec - 1));
3068 l_max = (1 << (l_img_comp->prec - 1)) - 1;
3072 l_max = (1 << l_img_comp->prec) - 1;
3075 l_current_ptr = l_tile_comp->data;
3077 if (l_tccp->qmfbid == 1) {
3078 for (j=0;j<l_height;++j) {
3079 for (i = 0; i < l_width; ++i) {
3080 *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
3083 l_current_ptr += l_stride;
3087 for (j=0;j<l_height;++j) {
3088 for (i = 0; i < l_width; ++i) {
3089 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
3090 *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
3093 l_current_ptr += l_stride;
3108 * Deallocates the encoding data of the given precinct.
3110 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct)
3112 OPJ_UINT32 cblkno , l_nb_code_blocks;
3114 opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec;
3116 /*fprintf(stderr,"deallocate codeblock:{\n");*/
3117 /*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);*/
3118 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
3119 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 );*/
3122 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t);
3123 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
3125 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
3127 if (l_code_block->data) {
3128 opj_free(l_code_block->data);
3129 l_code_block->data = 00;
3132 if (l_code_block->segs) {
3133 opj_free(l_code_block->segs );
3134 l_code_block->segs = 00;
3140 opj_free(p_precinct->cblks.dec);
3141 p_precinct->cblks.dec = 00;
3145 OPJ_UINT32 tcd_get_encoded_tile_size ( opj_tcd_v2_t *p_tcd )
3147 OPJ_UINT32 i,l_data_size = 0;
3148 opj_image_comp_t * l_img_comp = 00;
3149 opj_tcd_tilecomp_v2_t * l_tilec = 00;
3150 OPJ_UINT32 l_size_comp, l_remaining;
3152 l_tilec = p_tcd->tcd_image->tiles->comps;
3153 l_img_comp = p_tcd->image->comps;
3154 for (i=0;i<p_tcd->image->numcomps;++i) {
3155 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
3156 l_remaining = l_img_comp->prec & 7; /* (%8) */
3162 if (l_size_comp == 3) {
3166 l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
3174 opj_bool tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd )
3177 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
3178 opj_tccp_t * l_tccp = 00;
3179 opj_image_comp_t * l_img_comp = 00;
3180 opj_tcp_v2_t * l_tcp = 00;
3181 opj_tcd_tile_v2_t * l_tile;
3182 OPJ_UINT32 l_nb_elem,i;
3183 OPJ_INT32 * l_current_ptr;
3185 l_tile = p_tcd->tcd_image->tiles;
3186 l_tile_comp = l_tile->comps;
3188 l_tccp = p_tcd->tcp->tccps;
3189 l_img_comp = p_tcd->image->comps;
3191 for (compno = 0; compno < l_tile->numcomps; compno++) {
3192 l_current_ptr = l_tile_comp->data;
3193 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3195 if (l_tccp->qmfbid == 1) {
3196 for (i = 0; i < l_nb_elem; ++i) {
3197 *l_current_ptr -= l_tccp->m_dc_level_shift ;
3202 for (i = 0; i < l_nb_elem; ++i) {
3203 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
3216 opj_bool tcd_mct_encode ( opj_tcd_v2_t *p_tcd )
3218 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
3219 opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
3220 OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3222 OPJ_BYTE ** l_data = 00;
3223 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
3225 if(!p_tcd->tcp->mct) {
3229 if (p_tcd->tcp->mct == 2) {
3230 if (! p_tcd->tcp->m_mct_coding_matrix) {
3234 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
3239 for (i=0;i<l_tile->numcomps;++i) {
3240 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
3244 if (! mct_encode_custom(/* MCT data */
3245 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
3246 /* size of components */
3250 /* nb of components (i.e. size of pData) */
3252 /* tells if the data is signed */
3253 p_tcd->image->comps->sgnd) )
3261 else if (l_tcp->tccps->qmfbid == 0) {
3262 mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
3265 mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
3271 opj_bool tcd_dwt_encode ( opj_tcd_v2_t *p_tcd )
3273 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
3274 opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
3275 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
3278 for (compno = 0; compno < l_tile->numcomps; ++compno) {
3279 if (l_tccp->qmfbid == 1) {
3280 if (! dwt_encode_v2(l_tile_comp)) {
3284 else if (l_tccp->qmfbid == 0) {
3285 if (! dwt_encode_real_v2(l_tile_comp)) {
3297 opj_bool tcd_t1_encode ( opj_tcd_v2_t *p_tcd )
3300 const OPJ_FLOAT64 * l_mct_norms;
3301 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
3303 l_t1 = t1_create_v2();
3308 if (l_tcp->mct == 1) {
3309 /* irreversible encoding */
3310 if (l_tcp->tccps->qmfbid == 0) {
3311 l_mct_norms = get_mct_norms_real();
3314 l_mct_norms = get_mct_norms();
3318 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
3321 if (! t1_encode_cblks_v2(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
3322 t1_destroy_v2(l_t1);
3326 t1_destroy_v2(l_t1);
3331 opj_bool tcd_t2_encode (opj_tcd_v2_t *p_tcd,
3332 OPJ_BYTE * p_dest_data,
3333 OPJ_UINT32 * p_data_written,
3334 OPJ_UINT32 p_max_dest_size,
3335 opj_codestream_info_t *p_cstr_info )
3339 l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
3344 if (! t2_encode_packets_v2(
3347 p_tcd->tcd_image->tiles,
3348 p_tcd->tcp->numlayers,
3358 t2_destroy_v2(l_t2);
3362 t2_destroy_v2(l_t2);
3364 /*---------------CLEAN-------------------*/
3369 opj_bool tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd,
3370 OPJ_BYTE * p_dest_data,
3371 OPJ_UINT32 p_max_dest_size,
3372 opj_codestream_info_t *p_cstr_info )
3374 opj_cp_v2_t * l_cp = p_tcd->cp;
3375 OPJ_UINT32 l_nb_written = 0;
3378 p_cstr_info->index_write = 0;
3381 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
3383 /* Normal Rate/distortion allocation */
3384 if (! tcd_rateallocate_v2(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
3389 /* Fixed layer allocation */
3390 tcd_rateallocate_fixed_v2(p_tcd);
3397 opj_bool tcd_copy_tile_data ( opj_tcd_v2_t *p_tcd,
3399 OPJ_UINT32 p_src_length )
3401 OPJ_UINT32 i,j,l_data_size = 0;
3402 opj_image_comp_t * l_img_comp = 00;
3403 opj_tcd_tilecomp_v2_t * l_tilec = 00;
3404 OPJ_UINT32 l_size_comp, l_remaining;
3405 OPJ_UINT32 l_nb_elem;
3407 l_data_size = tcd_get_encoded_tile_size(p_tcd);
3408 if (l_data_size != p_src_length) {
3412 l_tilec = p_tcd->tcd_image->tiles->comps;
3413 l_img_comp = p_tcd->image->comps;
3414 for (i=0;i<p_tcd->image->numcomps;++i) {
3415 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
3416 l_remaining = l_img_comp->prec & 7; /* (%8) */
3417 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
3423 if (l_size_comp == 3) {
3427 switch (l_size_comp) {
3430 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
3431 OPJ_INT32 * l_dest_ptr = l_tilec->data;
3433 if (l_img_comp->sgnd) {
3434 for (j=0;j<l_nb_elem;++j) {
3435 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3439 for (j=0;j<l_nb_elem;++j) {
3440 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
3444 p_src = (OPJ_BYTE*) l_src_ptr;
3449 OPJ_INT32 * l_dest_ptr = l_tilec->data;
3450 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
3452 if (l_img_comp->sgnd) {
3453 for (j=0;j<l_nb_elem;++j) {
3454 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3458 for (j=0;j<l_nb_elem;++j) {
3459 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
3463 p_src = (OPJ_BYTE*) l_src_ptr;
3468 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
3469 OPJ_INT32 * l_dest_ptr = l_tilec->data;
3471 for (j=0;j<l_nb_elem;++j) {
3472 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3475 p_src = (OPJ_BYTE*) l_src_ptr;