2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16 * Copyright (c) 2012, CS Systemes d'Information, France
17 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include "opj_includes.h"
43 #include "opj_common.h"
45 /* ----------------------------------------------------------------------- */
49 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
51 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
53 fprintf(fd, "image {\n");
54 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
55 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
58 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
59 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
60 fprintf(fd, " tile {\n");
61 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
62 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
63 for (compno = 0; compno < tile->numcomps; compno++) {
64 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
65 fprintf(fd, " tilec {\n");
67 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
68 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
69 for (resno = 0; resno < tilec->numresolutions; resno++) {
70 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
71 fprintf(fd, "\n res {\n");
73 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
74 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
75 for (bandno = 0; bandno < res->numbands; bandno++) {
76 opj_tcd_band_t *band = &res->bands[bandno];
77 fprintf(fd, " band {\n");
79 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
80 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
81 for (precno = 0; precno < res->pw * res->ph; precno++) {
82 opj_tcd_precinct_t *prec = &band->precincts[precno];
83 fprintf(fd, " prec {\n");
85 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
86 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
88 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
89 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
90 fprintf(fd, " cblk {\n");
92 " x0=%d, y0=%d, x1=%d, y1=%d\n",
93 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
112 * Initializes tile coding/decoding
114 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
115 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
116 opj_event_mgr_t* manager);
119 * Allocates memory for a decoding code block.
121 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
125 * Deallocates the decoding data of the given precinct.
127 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct);
130 * Allocates memory for an encoding code block (but not data).
132 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
136 * Allocates data for an encoding code block
138 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
142 * Deallocates the encoding data of the given precinct.
144 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct);
148 Free the memory allocated for encoding
149 @param tcd TCD handle
151 static void opj_tcd_free_tile(opj_tcd_t *tcd);
154 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
155 OPJ_BYTE * p_src_data,
156 OPJ_UINT32 * p_data_read,
157 OPJ_UINT32 p_max_src_size,
158 opj_codestream_index_t *p_cstr_index,
159 opj_event_mgr_t *p_manager);
161 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd,
162 opj_event_mgr_t *p_manager);
164 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd);
166 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd,
167 opj_event_mgr_t *p_manager);
169 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd);
172 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd);
174 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd);
176 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd);
178 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd);
180 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
181 OPJ_BYTE * p_dest_data,
182 OPJ_UINT32 * p_data_written,
183 OPJ_UINT32 p_max_dest_size,
184 opj_codestream_info_t *p_cstr_info,
185 opj_event_mgr_t *p_manager);
187 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
188 OPJ_BYTE * p_dest_data,
189 OPJ_UINT32 p_max_dest_size,
190 opj_codestream_info_t *p_cstr_info,
191 opj_event_mgr_t *p_manager);
193 /* ----------------------------------------------------------------------- */
196 Create a new TCD handle
198 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
200 opj_tcd_t *l_tcd = 00;
202 /* create the tcd structure */
203 l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
208 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
210 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
211 if (!l_tcd->tcd_image) {
220 /* ----------------------------------------------------------------------- */
222 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
226 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
227 opj_tcd_makelayer_fixed(tcd, layno, 1);
232 void opj_tcd_makelayer(opj_tcd_t *tcd,
237 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
240 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
242 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
244 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
245 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
247 for (resno = 0; resno < tilec->numresolutions; resno++) {
248 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
250 for (bandno = 0; bandno < res->numbands; bandno++) {
251 opj_tcd_band_t *band = &res->bands[bandno];
253 /* Skip empty bands */
254 if (opj_tcd_is_band_empty(band)) {
258 for (precno = 0; precno < res->pw * res->ph; precno++) {
259 opj_tcd_precinct_t *prc = &band->precincts[precno];
261 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
262 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
263 opj_tcd_layer_t *layer = &cblk->layers[layno];
267 cblk->numpassesinlayers = 0;
270 n = cblk->numpassesinlayers;
273 /* Special value to indicate to use all passes */
274 n = cblk->totalpasses;
276 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
279 opj_tcd_pass_t *pass = &cblk->passes[passno];
283 dd = pass->distortiondec;
285 dr = pass->rate - cblk->passes[n - 1].rate;
286 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
295 if (thresh - (dd / dr) <
296 DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
302 layer->numpasses = n - cblk->numpassesinlayers;
304 if (!layer->numpasses) {
309 if (cblk->numpassesinlayers == 0) {
310 layer->len = cblk->passes[n - 1].rate;
311 layer->data = cblk->data;
312 layer->disto = cblk->passes[n - 1].distortiondec;
314 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
316 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
317 layer->disto = cblk->passes[n - 1].distortiondec -
318 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
321 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
324 cblk->numpassesinlayers = n;
333 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
336 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
337 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
338 OPJ_INT32 matrice[10][10][3];
341 opj_cp_t *cp = tcd->cp;
342 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
343 opj_tcp_t *tcd_tcp = tcd->tcp;
345 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
346 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
348 for (i = 0; i < tcd_tcp->numlayers; i++) {
349 for (j = 0; j < tilec->numresolutions; j++) {
350 for (k = 0; k < 3; k++) {
352 (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
353 tilec->numresolutions * 3 + j * 3 + k]
354 * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
359 for (resno = 0; resno < tilec->numresolutions; resno++) {
360 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
362 for (bandno = 0; bandno < res->numbands; bandno++) {
363 opj_tcd_band_t *band = &res->bands[bandno];
365 /* Skip empty bands */
366 if (opj_tcd_is_band_empty(band)) {
370 for (precno = 0; precno < res->pw * res->ph; precno++) {
371 opj_tcd_precinct_t *prc = &band->precincts[precno];
373 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
374 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
375 opj_tcd_layer_t *layer = &cblk->layers[layno];
377 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
378 cblk->numbps); /* number of bit-plan equal to zero */
380 /* Correction of the matrix of coefficient to include the IMSB information */
382 value = matrice[layno][resno][bandno];
389 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
390 if (imsb >= matrice[layno - 1][resno][bandno]) {
391 value -= (imsb - matrice[layno - 1][resno][bandno]);
399 cblk->numpassesinlayers = 0;
402 n = cblk->numpassesinlayers;
403 if (cblk->numpassesinlayers == 0) {
405 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
407 n = cblk->numpassesinlayers;
410 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
413 layer->numpasses = n - cblk->numpassesinlayers;
415 if (!layer->numpasses) {
419 if (cblk->numpassesinlayers == 0) {
420 layer->len = cblk->passes[n - 1].rate;
421 layer->data = cblk->data;
423 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
425 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
429 cblk->numpassesinlayers = n;
438 OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
440 OPJ_UINT32 * p_data_written,
442 opj_codestream_info_t *cstr_info,
443 opj_event_mgr_t *p_manager)
445 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
447 OPJ_FLOAT64 min, max;
448 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
449 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
450 OPJ_FLOAT64 maxSE = 0;
452 opj_cp_t *cp = tcd->cp;
453 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
454 opj_tcp_t *tcd_tcp = tcd->tcp;
459 tcd_tile->numpix = 0; /* fixed_quality */
461 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
462 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
465 for (resno = 0; resno < tilec->numresolutions; resno++) {
466 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
468 for (bandno = 0; bandno < res->numbands; bandno++) {
469 opj_tcd_band_t *band = &res->bands[bandno];
471 /* Skip empty bands */
472 if (opj_tcd_is_band_empty(band)) {
476 for (precno = 0; precno < res->pw * res->ph; precno++) {
477 opj_tcd_precinct_t *prc = &band->precincts[precno];
479 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
480 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
482 for (passno = 0; passno < cblk->totalpasses; passno++) {
483 opj_tcd_pass_t *pass = &cblk->passes[passno];
485 OPJ_FLOAT64 dd, rdslope;
488 dr = (OPJ_INT32)pass->rate;
489 dd = pass->distortiondec;
491 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
492 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
510 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
511 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
517 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
518 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
519 * ((OPJ_FLOAT64)(tilec->numpix));
524 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
525 tile_info->numpix = tcd_tile->numpix;
526 tile_info->distotile = tcd_tile->distotile;
527 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
529 if (!tile_info->thresh) {
530 /* FIXME event manager error callback */
535 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
536 OPJ_FLOAT64 lo = min;
537 OPJ_FLOAT64 hi = max;
538 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
539 OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
540 OPJ_FLOAT64 goodthresh = 0;
541 OPJ_FLOAT64 stable_thresh = 0;
542 OPJ_FLOAT64 old_thresh = -1;
543 const OPJ_FLOAT64 tolerance = 0.01;
545 OPJ_FLOAT64 distotarget; /* fixed_quality */
548 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
549 tcd_tcp->distoratio[layno] / 10));
551 /* Don't try to find an optimal threshold but rather take everything not included yet, if
552 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
553 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
554 ==> possible to have some lossy layers and the last layer for sure lossless */
555 if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
556 (tcd_tcp->rates[layno] > 0.0f)) ||
557 ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
558 (tcd_tcp->distoratio[layno] > 0.0))) {
559 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
560 OPJ_FLOAT64 thresh = 0;
566 for (i = 0; i < 128; ++i) {
567 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
569 thresh = (lo + hi) / 2;
571 opj_tcd_makelayer(tcd, layno, thresh, 0);
572 if ((fabs(old_thresh - thresh)) < tolerance)
576 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
577 if (OPJ_IS_CINEMA(cp->rsiz)) {
578 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
579 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
580 THRESH_CALC, p_manager)) {
585 distoachieved = layno == 0 ?
586 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
588 if (distoachieved < distotarget) {
590 stable_thresh = thresh;
597 distoachieved = (layno == 0) ?
598 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
600 if (distoachieved < distotarget) {
602 stable_thresh = thresh;
608 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
609 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
610 THRESH_CALC, p_manager)) {
611 /* TODO: what to do with l ??? seek / tell ??? */
612 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
618 stable_thresh = thresh;
622 goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
626 /* Special value to indicate to use all passes */
630 if (cstr_info) { /* Threshold for Marcela Index */
631 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
634 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
637 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
638 (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
644 OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
645 opj_image_t * p_image,
647 opj_thread_pool_t* p_tp)
649 p_tcd->image = p_image;
652 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
653 sizeof(opj_tcd_tile_t));
654 if (! p_tcd->tcd_image->tiles) {
658 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
659 p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
660 if (! p_tcd->tcd_image->tiles->comps) {
664 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
665 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
666 p_tcd->thread_pool = p_tp;
672 Destroy a previously created TCD handle
674 void opj_tcd_destroy(opj_tcd_t *tcd)
677 opj_tcd_free_tile(tcd);
679 if (tcd->tcd_image) {
680 opj_free(tcd->tcd_image);
687 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
689 if ((l_tilec->data == 00) ||
690 ((l_tilec->data_size_needed > l_tilec->data_size) &&
691 (l_tilec->ownsData == OPJ_FALSE))) {
692 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
693 if (! l_tilec->data) {
696 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
697 l_tilec->data_size = l_tilec->data_size_needed;
698 l_tilec->ownsData = OPJ_TRUE;
699 } else if (l_tilec->data_size_needed > l_tilec->data_size) {
700 /* We don't need to keep old data */
701 opj_image_data_free(l_tilec->data);
702 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
703 if (! l_tilec->data) {
704 l_tilec->data_size = 0;
705 l_tilec->data_size_needed = 0;
706 l_tilec->ownsData = OPJ_FALSE;
709 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
710 l_tilec->data_size = l_tilec->data_size_needed;
711 l_tilec->ownsData = OPJ_TRUE;
716 /* ----------------------------------------------------------------------- */
718 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
719 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
720 opj_event_mgr_t* manager)
722 OPJ_UINT32(*l_gain_ptr)(OPJ_UINT32) = 00;
723 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
724 opj_tcp_t * l_tcp = 00;
725 opj_cp_t * l_cp = 00;
726 opj_tcd_tile_t * l_tile = 00;
727 opj_tccp_t *l_tccp = 00;
728 opj_tcd_tilecomp_t *l_tilec = 00;
729 opj_image_comp_t * l_image_comp = 00;
730 opj_tcd_resolution_t *l_res = 00;
731 opj_tcd_band_t *l_band = 00;
732 opj_stepsize_t * l_step_size = 00;
733 opj_tcd_precinct_t *l_current_precinct = 00;
734 opj_image_t *l_image = 00;
736 OPJ_UINT32 l_level_no;
737 OPJ_UINT32 l_pdx, l_pdy;
739 OPJ_INT32 l_x0b, l_y0b;
740 OPJ_UINT32 l_tx0, l_ty0;
741 /* extent of precincts , top left, bottom right**/
742 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
743 /* number of precinct for a resolution */
744 OPJ_UINT32 l_nb_precincts;
745 /* room needed to store l_nb_precinct precinct for a resolution */
746 OPJ_UINT32 l_nb_precinct_size;
747 /* number of code blocks for a precinct*/
748 OPJ_UINT32 l_nb_code_blocks;
749 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
750 OPJ_UINT32 l_nb_code_blocks_size;
751 /* size of data for a tile */
752 OPJ_UINT32 l_data_size;
755 l_tcp = &(l_cp->tcps[p_tile_no]);
756 l_tile = p_tcd->tcd_image->tiles;
757 l_tccp = l_tcp->tccps;
758 l_tilec = l_tile->comps;
759 l_image = p_tcd->image;
760 l_image_comp = p_tcd->image->comps;
762 p = p_tile_no % l_cp->tw; /* tile coordinates */
763 q = p_tile_no / l_cp->tw;
764 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
766 /* 4 borders of the tile rescale on the image if necessary */
767 l_tx0 = l_cp->tx0 + p *
768 l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
769 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
770 l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
772 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
773 if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
774 opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
777 l_ty0 = l_cp->ty0 + q *
778 l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
779 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
780 l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
782 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
783 if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
784 opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
789 /* testcase 1888.pdf.asan.35.988 */
790 if (l_tccp->numresolutions == 0) {
791 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
794 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
796 /*tile->numcomps = image->numcomps; */
797 for (compno = 0; compno < l_tile->numcomps; ++compno) {
798 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
799 l_image_comp->resno_decoded = 0;
800 /* border of each l_tile component (global) */
801 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
802 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
803 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
804 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
805 l_tilec->compno = compno;
806 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
808 /* compute l_data_size with overflow check */
809 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
810 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
811 if ((l_data_size > 0U) &&
812 ((((OPJ_UINT32) - 1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 -
814 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
817 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
819 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
820 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
823 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
824 l_tilec->numresolutions = l_tccp->numresolutions;
825 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
826 l_tilec->minimum_num_resolutions = 1;
828 l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
829 l_cp->m_specific_param.m_dec.m_reduce;
832 l_tilec->data_size_needed = l_data_size;
833 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
834 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
838 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
839 opj_tcd_resolution_t);
841 if (l_tilec->resolutions == 00) {
842 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
843 if (! l_tilec->resolutions) {
846 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
847 l_tilec->resolutions_size = l_data_size;
848 memset(l_tilec->resolutions, 0, l_data_size);
849 } else if (l_data_size > l_tilec->resolutions_size) {
850 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
851 l_tilec->resolutions, l_data_size);
852 if (! new_resolutions) {
853 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
854 opj_free(l_tilec->resolutions);
855 l_tilec->resolutions = NULL;
856 l_tilec->resolutions_size = 0;
859 l_tilec->resolutions = new_resolutions;
860 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
861 memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
862 l_data_size - l_tilec->resolutions_size);
863 l_tilec->resolutions_size = l_data_size;
866 l_level_no = l_tilec->numresolutions;
867 l_res = l_tilec->resolutions;
868 l_step_size = l_tccp->stepsizes;
869 if (l_tccp->qmfbid == 0) {
870 l_gain_ptr = &opj_dwt_getgain_real;
872 l_gain_ptr = &opj_dwt_getgain;
874 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
876 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
877 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
878 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
879 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
880 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
884 /* border for each resolution level (global) */
885 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
886 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
887 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
888 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
889 /*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);*/
890 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
891 l_pdx = l_tccp->prcw[resno];
892 l_pdy = l_tccp->prch[resno];
893 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
894 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
895 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
896 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
897 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
898 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
899 /*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 );*/
901 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
902 l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
903 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
904 l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
905 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
907 if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
908 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
911 l_nb_precincts = l_res->pw * l_res->ph;
913 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
915 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
918 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
921 tlcbgxstart = l_tl_prc_x_start;
922 tlcbgystart = l_tl_prc_y_start;
923 /*brcbgxend = l_br_prc_x_end;*/
924 /* brcbgyend = l_br_prc_y_end;*/
925 cbgwidthexpn = l_pdx;
926 cbgheightexpn = l_pdy;
929 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
930 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
931 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
932 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
933 cbgwidthexpn = l_pdx - 1;
934 cbgheightexpn = l_pdy - 1;
938 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
939 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
940 l_band = l_res->bands;
942 for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
944 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
948 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
949 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
950 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
951 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
953 l_band->bandno = bandno + 1;
954 /* x0b = 1 if bandno = 1 or 3 */
955 l_x0b = l_band->bandno & 1;
956 /* y0b = 1 if bandno = 2 or 3 */
957 l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
958 /* l_band border (global) */
959 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
960 l_level_no), (OPJ_INT32)(l_level_no + 1));
961 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
962 l_level_no), (OPJ_INT32)(l_level_no + 1));
963 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
964 l_level_no), (OPJ_INT32)(l_level_no + 1));
965 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
966 l_level_no), (OPJ_INT32)(l_level_no + 1));
970 /* Skip empty bands */
971 if (opj_tcd_is_band_empty(l_band)) {
972 /* Do not zero l_band->precints to avoid leaks */
973 /* but make sure we don't use it later, since */
974 /* it will point to precincts of previous bands... */
979 /** avoid an if with storing function pointer */
980 l_gain = (*l_gain_ptr)(l_band->bandno);
981 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
982 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
983 (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
984 /* Mb value of Equation E-2 in "E.1 Inverse quantization
985 * procedure" of the standard */
986 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
989 if (!l_band->precincts && (l_nb_precincts > 0U)) {
990 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
992 if (! l_band->precincts) {
993 opj_event_msg(manager, EVT_ERROR,
994 "Not enough memory to handle band precints\n");
997 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
998 memset(l_band->precincts, 0, l_nb_precinct_size);
999 l_band->precincts_data_size = l_nb_precinct_size;
1000 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
1002 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
1003 l_band->precincts,/*3 * */ l_nb_precinct_size);
1004 if (! new_precincts) {
1005 opj_event_msg(manager, EVT_ERROR,
1006 "Not enough memory to handle band precints\n");
1007 opj_free(l_band->precincts);
1008 l_band->precincts = NULL;
1009 l_band->precincts_data_size = 0;
1012 l_band->precincts = new_precincts;
1013 /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/
1014 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
1015 l_nb_precinct_size - l_band->precincts_data_size);
1016 l_band->precincts_data_size = l_nb_precinct_size;
1019 l_current_precinct = l_band->precincts;
1020 for (precno = 0; precno < l_nb_precincts; ++precno) {
1021 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1022 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1023 (1 << cbgwidthexpn);
1024 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1025 (1 << cbgheightexpn);
1026 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1027 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1028 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1029 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1031 /* precinct size (global) */
1032 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1034 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1035 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1036 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1037 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1038 /*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);*/
1040 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1041 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1042 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1043 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1044 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1045 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1046 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1047 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1048 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1049 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1050 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1051 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1052 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1054 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1057 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1058 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
1059 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1061 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1062 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1063 if (! l_current_precinct->cblks.blocks) {
1066 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1068 memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1070 l_current_precinct->block_size = l_nb_code_blocks_size;
1071 } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1072 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1073 l_nb_code_blocks_size);
1075 opj_free(l_current_precinct->cblks.blocks);
1076 l_current_precinct->cblks.blocks = NULL;
1077 l_current_precinct->block_size = 0;
1078 opj_event_msg(manager, EVT_ERROR,
1079 "Not enough memory for current precinct codeblock element\n");
1082 l_current_precinct->cblks.blocks = new_blocks;
1083 /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */
1085 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1086 l_current_precinct->block_size
1088 , l_nb_code_blocks_size - l_current_precinct->block_size);
1090 l_current_precinct->block_size = l_nb_code_blocks_size;
1093 if (! l_current_precinct->incltree) {
1094 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1095 l_current_precinct->ch, manager);
1097 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1098 l_current_precinct->cw, l_current_precinct->ch, manager);
1101 if (! l_current_precinct->imsbtree) {
1102 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1103 l_current_precinct->ch, manager);
1105 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1106 l_current_precinct->cw, l_current_precinct->ch, manager);
1109 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1110 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1111 l_current_precinct->cw) * (1 << cblkwidthexpn);
1112 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1113 l_current_precinct->cw) * (1 << cblkheightexpn);
1114 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1115 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1118 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1120 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1123 /* code-block size (global) */
1124 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1125 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1126 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1127 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1129 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1133 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1135 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1138 /* code-block size (global) */
1139 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1140 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1141 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1142 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1145 ++l_current_precinct;
1157 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1158 opj_event_mgr_t* p_manager)
1160 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1161 sizeof(opj_tcd_cblk_enc_t), p_manager);
1164 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1165 opj_event_mgr_t* p_manager)
1167 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1168 sizeof(opj_tcd_cblk_dec_t), p_manager);
1172 * Allocates memory for an encoding code block (but not data memory).
1174 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1177 if (! p_code_block->layers) {
1178 /* no memset since data */
1179 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1180 sizeof(opj_tcd_layer_t));
1181 if (! p_code_block->layers) {
1185 if (! p_code_block->passes) {
1186 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1187 sizeof(opj_tcd_pass_t));
1188 if (! p_code_block->passes) {
1196 * Allocates data memory for an encoding code block.
1198 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1201 OPJ_UINT32 l_data_size;
1203 /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1204 /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
1205 /* TODO: is there a theoretical upper-bound for the compressed code */
1207 l_data_size = 2 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1208 (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1210 if (l_data_size > p_code_block->data_size) {
1211 if (p_code_block->data) {
1212 /* We refer to data - 1 since below we incremented it */
1213 opj_free(p_code_block->data - 1);
1215 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1216 if (! p_code_block->data) {
1217 p_code_block->data_size = 0U;
1220 p_code_block->data_size = l_data_size;
1222 /* We reserve the initial byte as a fake byte to a non-FF value */
1223 /* and increment the data pointer, so that opj_mqc_init_enc() */
1224 /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1226 p_code_block->data[0] = 0;
1227 p_code_block->data += 1; /*why +1 ?*/
1233 void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1235 memset(seg, 0, sizeof(opj_tcd_seg_t));
1239 * Allocates memory for a decoding code block.
1241 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1244 if (! p_code_block->segs) {
1246 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1247 sizeof(opj_tcd_seg_t));
1248 if (! p_code_block->segs) {
1251 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1253 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1254 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1257 opj_tcd_seg_t * l_segs = p_code_block->segs;
1258 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1259 opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
1260 OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
1263 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1264 p_code_block->segs = l_segs;
1265 p_code_block->m_current_max_segs = l_current_max_segs;
1266 for (i = 0; i < l_current_max_segs; ++i) {
1267 opj_tcd_reinit_segment(&l_segs[i]);
1269 p_code_block->chunks = l_chunks;
1270 p_code_block->numchunksalloc = l_numchunksalloc;
1276 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd)
1279 OPJ_UINT32 l_data_size = 0;
1280 opj_image_comp_t * l_img_comp = 00;
1281 opj_tcd_tilecomp_t * l_tile_comp = 00;
1282 opj_tcd_resolution_t * l_res = 00;
1283 OPJ_UINT32 l_size_comp, l_remaining;
1286 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1287 l_img_comp = p_tcd->image->comps;
1289 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1290 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1291 l_remaining = l_img_comp->prec & 7; /* (%8) */
1297 if (l_size_comp == 3) {
1301 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1302 l_temp = (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 -
1303 l_res->y0)); /* x1*y1 can't overflow */
1304 if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1307 l_temp *= l_size_comp;
1309 if (l_temp > UINT_MAX - l_data_size) {
1312 l_data_size += l_temp;
1320 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1321 OPJ_UINT32 p_tile_no,
1323 OPJ_UINT32 * p_data_written,
1324 OPJ_UINT32 p_max_length,
1325 opj_codestream_info_t *p_cstr_info,
1326 opj_event_mgr_t *p_manager)
1329 if (p_tcd->cur_tp_num == 0) {
1331 p_tcd->tcd_tileno = p_tile_no;
1332 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1334 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1336 OPJ_UINT32 l_num_packs = 0;
1338 opj_tcd_tilecomp_t *l_tilec_idx =
1339 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1340 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1342 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1343 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1345 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1346 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1348 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1349 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1350 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1352 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1353 size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs,
1354 sizeof(opj_packet_info_t));
1355 if (!p_cstr_info->tile[p_tile_no].packet) {
1356 /* FIXME event manager error callback */
1362 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1363 /*---------------TILE-------------------*/
1364 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1367 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1369 /* FIXME _ProfStart(PGROUP_MCT); */
1370 if (! opj_tcd_mct_encode(p_tcd)) {
1373 /* FIXME _ProfStop(PGROUP_MCT); */
1375 /* FIXME _ProfStart(PGROUP_DWT); */
1376 if (! opj_tcd_dwt_encode(p_tcd)) {
1379 /* FIXME _ProfStop(PGROUP_DWT); */
1381 /* FIXME _ProfStart(PGROUP_T1); */
1382 if (! opj_tcd_t1_encode(p_tcd)) {
1385 /* FIXME _ProfStop(PGROUP_T1); */
1387 /* FIXME _ProfStart(PGROUP_RATE); */
1388 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
1389 p_cstr_info, p_manager)) {
1392 /* FIXME _ProfStop(PGROUP_RATE); */
1395 /*--------------TIER2------------------*/
1399 p_cstr_info->index_write = 1;
1401 /* FIXME _ProfStart(PGROUP_T2); */
1403 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1404 p_cstr_info, p_manager)) {
1407 /* FIXME _ProfStop(PGROUP_T2); */
1409 /*---------------CLEAN-------------------*/
1414 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1415 OPJ_UINT32 decoded_x0,
1416 OPJ_UINT32 decoded_y0,
1417 OPJ_UINT32 decoded_x1,
1418 OPJ_UINT32 decoded_y1,
1420 OPJ_UINT32 p_max_length,
1421 OPJ_UINT32 p_tile_no,
1422 opj_codestream_index_t *p_cstr_index,
1423 opj_event_mgr_t *p_manager
1426 OPJ_UINT32 l_data_read;
1427 p_tcd->tcd_tileno = p_tile_no;
1428 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1429 p_tcd->decoded_x0 = decoded_x0;
1430 p_tcd->decoded_y0 = decoded_y0;
1431 p_tcd->decoded_x1 = decoded_x1;
1432 p_tcd->decoded_y1 = decoded_y1;
1434 #ifdef TODO_MSD /* FIXME */
1437 OPJ_UINT32 resno, compno, numprec = 0;
1438 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1439 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1440 opj_tccp_t *tccp = &tcp->tccps[compno];
1441 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1442 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1443 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1444 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1445 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1446 numprec += res_idx->pw * res_idx->ph;
1447 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1448 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1451 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1452 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1453 p_cstr_info->packno = 0;
1458 /*--------------TIER2------------------*/
1459 /* FIXME _ProfStart(PGROUP_T2); */
1461 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1465 /* FIXME _ProfStop(PGROUP_T2); */
1467 /*------------------TIER1-----------------*/
1469 /* FIXME _ProfStart(PGROUP_T1); */
1470 if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1473 /* FIXME _ProfStop(PGROUP_T1); */
1475 /*----------------DWT---------------------*/
1477 /* FIXME _ProfStart(PGROUP_DWT); */
1479 (! opj_tcd_dwt_decode(p_tcd)) {
1482 /* FIXME _ProfStop(PGROUP_DWT); */
1484 /*----------------MCT-------------------*/
1485 /* FIXME _ProfStart(PGROUP_MCT); */
1487 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1490 /* FIXME _ProfStop(PGROUP_MCT); */
1492 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1494 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1497 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1500 /*---------------TILE-------------------*/
1504 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1506 OPJ_UINT32 p_dest_length
1509 OPJ_UINT32 i, j, k, l_data_size = 0;
1510 opj_image_comp_t * l_img_comp = 00;
1511 opj_tcd_tilecomp_t * l_tilec = 00;
1512 opj_tcd_resolution_t * l_res;
1513 OPJ_UINT32 l_size_comp, l_remaining;
1514 OPJ_UINT32 l_stride, l_width, l_height;
1516 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1517 if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1521 l_tilec = p_tcd->tcd_image->tiles->comps;
1522 l_img_comp = p_tcd->image->comps;
1524 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1525 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1526 l_remaining = l_img_comp->prec & 7; /* (%8) */
1527 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1528 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1529 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1530 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1536 if (l_size_comp == 3) {
1540 switch (l_size_comp) {
1542 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1543 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1545 if (l_img_comp->sgnd) {
1546 for (j = 0; j < l_height; ++j) {
1547 for (k = 0; k < l_width; ++k) {
1548 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1550 l_src_ptr += l_stride;
1553 for (j = 0; j < l_height; ++j) {
1554 for (k = 0; k < l_width; ++k) {
1555 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1557 l_src_ptr += l_stride;
1561 p_dest = (OPJ_BYTE *)l_dest_ptr;
1565 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1566 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1568 if (l_img_comp->sgnd) {
1569 for (j = 0; j < l_height; ++j) {
1570 for (k = 0; k < l_width; ++k) {
1571 OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1572 memcpy(l_dest_ptr, &val, sizeof(val));
1575 l_src_ptr += l_stride;
1578 for (j = 0; j < l_height; ++j) {
1579 for (k = 0; k < l_width; ++k) {
1580 OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1581 memcpy(l_dest_ptr, &val, sizeof(val));
1584 l_src_ptr += l_stride;
1588 p_dest = (OPJ_BYTE*) l_dest_ptr;
1592 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1593 OPJ_INT32 * l_src_ptr = l_tilec->data;
1595 for (j = 0; j < l_height; ++j) {
1596 memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1597 l_dest_ptr += l_width;
1598 l_src_ptr += l_width + l_stride;
1601 p_dest = (OPJ_BYTE*) l_dest_ptr;
1616 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1618 OPJ_UINT32 compno, resno, bandno, precno;
1619 opj_tcd_tile_t *l_tile = 00;
1620 opj_tcd_tilecomp_t *l_tile_comp = 00;
1621 opj_tcd_resolution_t *l_res = 00;
1622 opj_tcd_band_t *l_band = 00;
1623 opj_tcd_precinct_t *l_precinct = 00;
1624 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1625 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1631 if (! p_tcd->tcd_image) {
1635 if (p_tcd->m_is_decoder) {
1636 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1638 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1641 l_tile = p_tcd->tcd_image->tiles;
1646 l_tile_comp = l_tile->comps;
1648 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1649 l_res = l_tile_comp->resolutions;
1652 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1653 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1654 l_band = l_res->bands;
1655 for (bandno = 0; bandno < 3; ++bandno) {
1656 l_precinct = l_band->precincts;
1659 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1660 for (precno = 0; precno < l_nb_precincts; ++precno) {
1661 opj_tgt_destroy(l_precinct->incltree);
1662 l_precinct->incltree = 00;
1663 opj_tgt_destroy(l_precinct->imsbtree);
1664 l_precinct->imsbtree = 00;
1665 (*l_tcd_code_block_deallocate)(l_precinct);
1669 opj_free(l_band->precincts);
1670 l_band->precincts = 00;
1677 opj_free(l_tile_comp->resolutions);
1678 l_tile_comp->resolutions = 00;
1681 if (l_tile_comp->ownsData && l_tile_comp->data) {
1682 opj_image_data_free(l_tile_comp->data);
1683 l_tile_comp->data = 00;
1684 l_tile_comp->ownsData = 0;
1685 l_tile_comp->data_size = 0;
1686 l_tile_comp->data_size_needed = 0;
1691 opj_free(l_tile->comps);
1693 opj_free(p_tcd->tcd_image->tiles);
1694 p_tcd->tcd_image->tiles = 00;
1698 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1699 OPJ_BYTE * p_src_data,
1700 OPJ_UINT32 * p_data_read,
1701 OPJ_UINT32 p_max_src_size,
1702 opj_codestream_index_t *p_cstr_index,
1703 opj_event_mgr_t *p_manager
1708 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1713 if (! opj_t2_decode_packets(
1717 p_tcd->tcd_image->tiles,
1723 opj_t2_destroy(l_t2);
1727 opj_t2_destroy(l_t2);
1729 /*---------------CLEAN-------------------*/
1733 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1736 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1737 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1738 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1739 volatile OPJ_BOOL ret = OPJ_TRUE;
1740 OPJ_BOOL check_pterm = OPJ_FALSE;
1741 opj_mutex_t* p_manager_mutex = NULL;
1743 p_manager_mutex = opj_mutex_create();
1745 /* Only enable PTERM check if we decode all layers */
1746 if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
1747 (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
1748 check_pterm = OPJ_TRUE;
1751 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1752 opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp,
1753 p_manager, p_manager_mutex, check_pterm);
1761 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1762 if (p_manager_mutex) {
1763 opj_mutex_destroy(p_manager_mutex);
1769 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1772 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1773 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1774 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1775 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1777 for (compno = 0; compno < l_tile->numcomps; compno++) {
1779 if (tcd->cp->reduce != 0) {
1780 tcd->image->comps[compno].resno_decoded =
1781 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1782 if (tcd->image->comps[compno].resno_decoded < 0)
1787 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1788 if(numres2decode > 0){
1791 if (l_tccp->qmfbid == 1) {
1792 if (! opj_dwt_decode(p_tcd, l_tile_comp,
1793 l_img_comp->resno_decoded + 1)) {
1797 if (! opj_dwt_decode_real(p_tcd, l_tile_comp,
1798 l_img_comp->resno_decoded + 1)) {
1810 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1812 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1813 opj_tcp_t * l_tcp = p_tcd->tcp;
1814 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1815 OPJ_UINT32 l_samples, i;
1821 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1822 (l_tile_comp->y1 - l_tile_comp->y0));
1824 if (l_tile->numcomps >= 3) {
1825 /* testcase 1336.pdf.asan.47.376 */
1826 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 -
1827 l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1828 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 -
1829 l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1830 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 -
1831 l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1832 opj_event_msg(p_manager, EVT_ERROR,
1833 "Tiles don't all have the same dimension. Skip the MCT step.\n");
1835 } else if (l_tcp->mct == 2) {
1838 if (! l_tcp->m_mct_decoding_matrix) {
1842 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
1847 for (i = 0; i < l_tile->numcomps; ++i) {
1848 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1852 if (! opj_mct_decode_custom(/* MCT data */
1853 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1854 /* size of components */
1858 /* nb of components (i.e. size of pData) */
1860 /* tells if the data is signed */
1861 p_tcd->image->comps->sgnd)) {
1868 if (l_tcp->tccps->qmfbid == 1) {
1869 opj_mct_decode(l_tile->comps[0].data,
1870 l_tile->comps[1].data,
1871 l_tile->comps[2].data,
1874 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1875 (OPJ_FLOAT32*)l_tile->comps[1].data,
1876 (OPJ_FLOAT32*)l_tile->comps[2].data,
1881 opj_event_msg(p_manager, EVT_ERROR,
1882 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
1890 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
1893 opj_tcd_tilecomp_t * l_tile_comp = 00;
1894 opj_tccp_t * l_tccp = 00;
1895 opj_image_comp_t * l_img_comp = 00;
1896 opj_tcd_resolution_t* l_res = 00;
1897 opj_tcd_tile_t * l_tile;
1898 OPJ_UINT32 l_width, l_height, i, j;
1899 OPJ_INT32 * l_current_ptr;
1900 OPJ_INT32 l_min, l_max;
1901 OPJ_UINT32 l_stride;
1903 l_tile = p_tcd->tcd_image->tiles;
1904 l_tile_comp = l_tile->comps;
1905 l_tccp = p_tcd->tcp->tccps;
1906 l_img_comp = p_tcd->image->comps;
1908 for (compno = 0; compno < l_tile->numcomps; compno++) {
1909 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1910 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1911 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1912 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1914 assert(l_height == 0 ||
1915 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1917 if (l_img_comp->sgnd) {
1918 l_min = -(1 << (l_img_comp->prec - 1));
1919 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1922 l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
1925 l_current_ptr = l_tile_comp->data;
1927 if (l_tccp->qmfbid == 1) {
1928 for (j = 0; j < l_height; ++j) {
1929 for (i = 0; i < l_width; ++i) {
1930 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
1934 l_current_ptr += l_stride;
1937 for (j = 0; j < l_height; ++j) {
1938 for (i = 0; i < l_width; ++i) {
1939 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1940 OPJ_INT32 l_value_int = (OPJ_INT32)opj_lrintf(l_value);
1941 if (l_value > INT_MAX ||
1942 (l_value_int > 0 && l_tccp->m_dc_level_shift > 0 &&
1943 l_value_int > INT_MAX - l_tccp->m_dc_level_shift)) {
1944 *l_current_ptr = l_max;
1946 *l_current_ptr = opj_int_clamp(
1947 l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
1951 l_current_ptr += l_stride;
1966 * Deallocates the encoding data of the given precinct.
1968 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
1970 OPJ_UINT32 cblkno, l_nb_code_blocks;
1972 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1974 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1975 /*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);*/
1976 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1977 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 );*/
1980 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1981 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1983 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1985 if (l_code_block->segs) {
1986 opj_free(l_code_block->segs);
1987 l_code_block->segs = 00;
1990 if (l_code_block->chunks) {
1991 opj_free(l_code_block->chunks);
1992 l_code_block->chunks = 00;
1998 opj_free(p_precinct->cblks.dec);
1999 p_precinct->cblks.dec = 00;
2004 * Deallocates the encoding data of the given precinct.
2006 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
2008 OPJ_UINT32 cblkno, l_nb_code_blocks;
2010 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
2012 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
2014 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2015 if (l_code_block->data) {
2016 /* We refer to data - 1 since below we incremented it */
2017 /* in opj_tcd_code_block_enc_allocate_data() */
2018 opj_free(l_code_block->data - 1);
2019 l_code_block->data = 00;
2022 if (l_code_block->layers) {
2023 opj_free(l_code_block->layers);
2024 l_code_block->layers = 00;
2027 if (l_code_block->passes) {
2028 opj_free(l_code_block->passes);
2029 l_code_block->passes = 00;
2034 opj_free(p_precinct->cblks.enc);
2036 p_precinct->cblks.enc = 00;
2040 OPJ_UINT32 opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
2042 OPJ_UINT32 i, l_data_size = 0;
2043 opj_image_comp_t * l_img_comp = 00;
2044 opj_tcd_tilecomp_t * l_tilec = 00;
2045 OPJ_UINT32 l_size_comp, l_remaining;
2047 l_tilec = p_tcd->tcd_image->tiles->comps;
2048 l_img_comp = p_tcd->image->comps;
2049 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2050 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2051 l_remaining = l_img_comp->prec & 7; /* (%8) */
2057 if (l_size_comp == 3) {
2061 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) *
2062 (l_tilec->y1 - l_tilec->y0));
2070 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2073 opj_tcd_tilecomp_t * l_tile_comp = 00;
2074 opj_tccp_t * l_tccp = 00;
2075 opj_image_comp_t * l_img_comp = 00;
2076 opj_tcd_tile_t * l_tile;
2077 OPJ_UINT32 l_nb_elem, i;
2078 OPJ_INT32 * l_current_ptr;
2080 l_tile = p_tcd->tcd_image->tiles;
2081 l_tile_comp = l_tile->comps;
2082 l_tccp = p_tcd->tcp->tccps;
2083 l_img_comp = p_tcd->image->comps;
2085 for (compno = 0; compno < l_tile->numcomps; compno++) {
2086 l_current_ptr = l_tile_comp->data;
2087 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2088 (l_tile_comp->y1 - l_tile_comp->y0));
2090 if (l_tccp->qmfbid == 1) {
2091 for (i = 0; i < l_nb_elem; ++i) {
2092 *l_current_ptr -= l_tccp->m_dc_level_shift ;
2096 for (i = 0; i < l_nb_elem; ++i) {
2097 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2110 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2112 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2113 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2114 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2115 (l_tile_comp->y1 - l_tile_comp->y0));
2117 OPJ_BYTE ** l_data = 00;
2118 opj_tcp_t * l_tcp = p_tcd->tcp;
2120 if (!p_tcd->tcp->mct) {
2124 if (p_tcd->tcp->mct == 2) {
2125 if (! p_tcd->tcp->m_mct_coding_matrix) {
2129 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2134 for (i = 0; i < l_tile->numcomps; ++i) {
2135 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2139 if (! opj_mct_encode_custom(/* MCT data */
2140 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2141 /* size of components */
2145 /* nb of components (i.e. size of pData) */
2147 /* tells if the data is signed */
2148 p_tcd->image->comps->sgnd)) {
2154 } else if (l_tcp->tccps->qmfbid == 0) {
2155 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2156 l_tile->comps[2].data, samples);
2158 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2159 l_tile->comps[2].data, samples);
2165 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2167 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2168 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2169 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2172 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2173 if (l_tccp->qmfbid == 1) {
2174 if (! opj_dwt_encode(l_tile_comp)) {
2177 } else if (l_tccp->qmfbid == 0) {
2178 if (! opj_dwt_encode_real(l_tile_comp)) {
2190 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2193 const OPJ_FLOAT64 * l_mct_norms;
2194 OPJ_UINT32 l_mct_numcomps = 0U;
2195 opj_tcp_t * l_tcp = p_tcd->tcp;
2197 l_t1 = opj_t1_create(OPJ_TRUE);
2202 if (l_tcp->mct == 1) {
2203 l_mct_numcomps = 3U;
2204 /* irreversible encoding */
2205 if (l_tcp->tccps->qmfbid == 0) {
2206 l_mct_norms = opj_mct_get_mct_norms_real();
2208 l_mct_norms = opj_mct_get_mct_norms();
2211 l_mct_numcomps = p_tcd->image->numcomps;
2212 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2215 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2217 opj_t1_destroy(l_t1);
2221 opj_t1_destroy(l_t1);
2226 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2227 OPJ_BYTE * p_dest_data,
2228 OPJ_UINT32 * p_data_written,
2229 OPJ_UINT32 p_max_dest_size,
2230 opj_codestream_info_t *p_cstr_info,
2231 opj_event_mgr_t *p_manager)
2235 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2240 if (! opj_t2_encode_packets(
2243 p_tcd->tcd_image->tiles,
2244 p_tcd->tcp->numlayers,
2254 opj_t2_destroy(l_t2);
2258 opj_t2_destroy(l_t2);
2260 /*---------------CLEAN-------------------*/
2265 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2266 OPJ_BYTE * p_dest_data,
2267 OPJ_UINT32 p_max_dest_size,
2268 opj_codestream_info_t *p_cstr_info,
2269 opj_event_mgr_t *p_manager)
2271 opj_cp_t * l_cp = p_tcd->cp;
2272 OPJ_UINT32 l_nb_written = 0;
2275 p_cstr_info->index_write = 0;
2278 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2279 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2281 /* Normal Rate/distortion allocation */
2282 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2283 p_cstr_info, p_manager)) {
2287 /* Fixed layer allocation */
2288 opj_tcd_rateallocate_fixed(p_tcd);
2295 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2297 OPJ_UINT32 p_src_length)
2299 OPJ_UINT32 i, j, l_data_size = 0;
2300 opj_image_comp_t * l_img_comp = 00;
2301 opj_tcd_tilecomp_t * l_tilec = 00;
2302 OPJ_UINT32 l_size_comp, l_remaining;
2303 OPJ_UINT32 l_nb_elem;
2305 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2306 if (l_data_size != p_src_length) {
2310 l_tilec = p_tcd->tcd_image->tiles->comps;
2311 l_img_comp = p_tcd->image->comps;
2312 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2313 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2314 l_remaining = l_img_comp->prec & 7; /* (%8) */
2315 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 -
2322 if (l_size_comp == 3) {
2326 switch (l_size_comp) {
2328 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2329 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2331 if (l_img_comp->sgnd) {
2332 for (j = 0; j < l_nb_elem; ++j) {
2333 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2336 for (j = 0; j < l_nb_elem; ++j) {
2337 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2341 p_src = (OPJ_BYTE*) l_src_ptr;
2345 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2346 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2348 if (l_img_comp->sgnd) {
2349 for (j = 0; j < l_nb_elem; ++j) {
2350 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2353 for (j = 0; j < l_nb_elem; ++j) {
2354 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2358 p_src = (OPJ_BYTE*) l_src_ptr;
2362 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2363 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2365 for (j = 0; j < l_nb_elem; ++j) {
2366 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2369 p_src = (OPJ_BYTE*) l_src_ptr;
2381 OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2383 return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);
2386 OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd,
2395 /* Note: those values for filter_margin are in part the result of */
2396 /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
2397 /* to the maximum left/right extension given in tables F.2 and F.3 of the */
2398 /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
2399 /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
2400 /* needed to be bumped to 4, in case inconsistencies are found while */
2401 /* decoding parts of irreversible coded images. */
2402 /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
2403 OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3;
2404 opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]);
2405 opj_image_comp_t* image_comp = &(tcd->image->comps[compno]);
2406 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2407 /* with the tile coordinates */
2408 OPJ_UINT32 tcx0 = opj_uint_max(
2409 (OPJ_UINT32)tilec->x0,
2410 opj_uint_ceildiv(tcd->decoded_x0, image_comp->dx));
2411 OPJ_UINT32 tcy0 = opj_uint_max(
2412 (OPJ_UINT32)tilec->y0,
2413 opj_uint_ceildiv(tcd->decoded_y0, image_comp->dy));
2414 OPJ_UINT32 tcx1 = opj_uint_min(
2415 (OPJ_UINT32)tilec->x1,
2416 opj_uint_ceildiv(tcd->decoded_x1, image_comp->dx));
2417 OPJ_UINT32 tcy1 = opj_uint_min(
2418 (OPJ_UINT32)tilec->y1,
2419 opj_uint_ceildiv(tcd->decoded_y1, image_comp->dy));
2420 /* Compute number of decomposition for this band. See table F-1 */
2421 OPJ_UINT32 nb = (resno == 0) ?
2422 tilec->numresolutions - 1 :
2423 tilec->numresolutions - resno;
2424 /* Map above tile-based coordinates to sub-band-based coordinates per */
2425 /* equation B-15 of the standard */
2426 OPJ_UINT32 x0b = bandno & 1;
2427 OPJ_UINT32 y0b = bandno >> 1;
2428 OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 :
2429 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2430 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2431 OPJ_UINT32 tby0 = (nb == 0) ? tcy0 :
2432 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2433 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2434 OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 :
2435 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2436 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2437 OPJ_UINT32 tby1 = (nb == 0) ? tcy1 :
2438 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2439 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2440 OPJ_BOOL intersects;
2442 if (tbx0 < filter_margin) {
2445 tbx0 -= filter_margin;
2447 if (tby0 < filter_margin) {
2450 tby0 -= filter_margin;
2452 tbx1 = opj_uint_adds(tbx1, filter_margin);
2453 tby1 = opj_uint_adds(tby1, filter_margin);
2455 intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 &&
2458 #ifdef DEBUG_VERBOSE
2459 printf("compno=%u resno=%u nb=%u bandno=%u x0b=%u y0b=%u band=%u,%u,%u,%u tb=%u,%u,%u,%u -> %u\n",
2460 compno, resno, nb, bandno, x0b, y0b,
2461 band_x0, band_y0, band_x1, band_y1,
2462 tbx0, tby0, tbx1, tby1, intersects);