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);
194 static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *tcd,
197 /* ----------------------------------------------------------------------- */
200 Create a new TCD handle
202 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
204 opj_tcd_t *l_tcd = 00;
206 /* create the tcd structure */
207 l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
212 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
214 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
215 if (!l_tcd->tcd_image) {
224 /* ----------------------------------------------------------------------- */
226 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
230 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
231 opj_tcd_makelayer_fixed(tcd, layno, 1);
236 void opj_tcd_makelayer(opj_tcd_t *tcd,
241 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
244 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
246 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
248 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
249 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
251 for (resno = 0; resno < tilec->numresolutions; resno++) {
252 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
254 for (bandno = 0; bandno < res->numbands; bandno++) {
255 opj_tcd_band_t *band = &res->bands[bandno];
257 /* Skip empty bands */
258 if (opj_tcd_is_band_empty(band)) {
262 for (precno = 0; precno < res->pw * res->ph; precno++) {
263 opj_tcd_precinct_t *prc = &band->precincts[precno];
265 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
266 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
267 opj_tcd_layer_t *layer = &cblk->layers[layno];
271 cblk->numpassesinlayers = 0;
274 n = cblk->numpassesinlayers;
277 /* Special value to indicate to use all passes */
278 n = cblk->totalpasses;
280 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
283 opj_tcd_pass_t *pass = &cblk->passes[passno];
287 dd = pass->distortiondec;
289 dr = pass->rate - cblk->passes[n - 1].rate;
290 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
299 if (thresh - (dd / dr) <
300 DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
306 layer->numpasses = n - cblk->numpassesinlayers;
308 if (!layer->numpasses) {
313 if (cblk->numpassesinlayers == 0) {
314 layer->len = cblk->passes[n - 1].rate;
315 layer->data = cblk->data;
316 layer->disto = cblk->passes[n - 1].distortiondec;
318 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
320 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
321 layer->disto = cblk->passes[n - 1].distortiondec -
322 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
325 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
328 cblk->numpassesinlayers = n;
337 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
340 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
341 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
342 OPJ_INT32 matrice[10][10][3];
345 opj_cp_t *cp = tcd->cp;
346 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
347 opj_tcp_t *tcd_tcp = tcd->tcp;
349 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
350 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
352 for (i = 0; i < tcd_tcp->numlayers; i++) {
353 for (j = 0; j < tilec->numresolutions; j++) {
354 for (k = 0; k < 3; k++) {
356 (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
357 tilec->numresolutions * 3 + j * 3 + k]
358 * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
363 for (resno = 0; resno < tilec->numresolutions; resno++) {
364 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
366 for (bandno = 0; bandno < res->numbands; bandno++) {
367 opj_tcd_band_t *band = &res->bands[bandno];
369 /* Skip empty bands */
370 if (opj_tcd_is_band_empty(band)) {
374 for (precno = 0; precno < res->pw * res->ph; precno++) {
375 opj_tcd_precinct_t *prc = &band->precincts[precno];
377 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
378 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
379 opj_tcd_layer_t *layer = &cblk->layers[layno];
381 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
382 cblk->numbps); /* number of bit-plan equal to zero */
384 /* Correction of the matrix of coefficient to include the IMSB information */
386 value = matrice[layno][resno][bandno];
393 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
394 if (imsb >= matrice[layno - 1][resno][bandno]) {
395 value -= (imsb - matrice[layno - 1][resno][bandno]);
403 cblk->numpassesinlayers = 0;
406 n = cblk->numpassesinlayers;
407 if (cblk->numpassesinlayers == 0) {
409 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
411 n = cblk->numpassesinlayers;
414 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
417 layer->numpasses = n - cblk->numpassesinlayers;
419 if (!layer->numpasses) {
423 if (cblk->numpassesinlayers == 0) {
424 layer->len = cblk->passes[n - 1].rate;
425 layer->data = cblk->data;
427 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
429 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
433 cblk->numpassesinlayers = n;
442 OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
444 OPJ_UINT32 * p_data_written,
446 opj_codestream_info_t *cstr_info,
447 opj_event_mgr_t *p_manager)
449 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
451 OPJ_FLOAT64 min, max;
452 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
453 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
454 OPJ_FLOAT64 maxSE = 0;
456 opj_cp_t *cp = tcd->cp;
457 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
458 opj_tcp_t *tcd_tcp = tcd->tcp;
463 tcd_tile->numpix = 0; /* fixed_quality */
465 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
466 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
469 for (resno = 0; resno < tilec->numresolutions; resno++) {
470 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
472 for (bandno = 0; bandno < res->numbands; bandno++) {
473 opj_tcd_band_t *band = &res->bands[bandno];
475 /* Skip empty bands */
476 if (opj_tcd_is_band_empty(band)) {
480 for (precno = 0; precno < res->pw * res->ph; precno++) {
481 opj_tcd_precinct_t *prc = &band->precincts[precno];
483 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
484 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
486 for (passno = 0; passno < cblk->totalpasses; passno++) {
487 opj_tcd_pass_t *pass = &cblk->passes[passno];
489 OPJ_FLOAT64 dd, rdslope;
492 dr = (OPJ_INT32)pass->rate;
493 dd = pass->distortiondec;
495 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
496 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
514 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
515 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
521 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
522 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
523 * ((OPJ_FLOAT64)(tilec->numpix));
528 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
529 tile_info->numpix = tcd_tile->numpix;
530 tile_info->distotile = tcd_tile->distotile;
531 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
533 if (!tile_info->thresh) {
534 /* FIXME event manager error callback */
539 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
540 OPJ_FLOAT64 lo = min;
541 OPJ_FLOAT64 hi = max;
542 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
543 OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
544 OPJ_FLOAT64 goodthresh = 0;
545 OPJ_FLOAT64 stable_thresh = 0;
547 OPJ_FLOAT64 distotarget; /* fixed_quality */
550 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
551 tcd_tcp->distoratio[layno] / 10));
553 /* Don't try to find an optimal threshold but rather take everything not included yet, if
554 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
555 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
556 ==> possible to have some lossy layers and the last layer for sure lossless */
557 if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
558 (tcd_tcp->rates[layno] > 0.0f)) ||
559 ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
560 (tcd_tcp->distoratio[layno] > 0.0))) {
561 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
562 OPJ_FLOAT64 thresh = 0;
568 for (i = 0; i < 128; ++i) {
569 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
571 thresh = (lo + hi) / 2;
573 opj_tcd_makelayer(tcd, layno, thresh, 0);
575 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
576 if (OPJ_IS_CINEMA(cp->rsiz)) {
577 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
578 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
579 THRESH_CALC, p_manager)) {
584 distoachieved = layno == 0 ?
585 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
587 if (distoachieved < distotarget) {
589 stable_thresh = thresh;
596 distoachieved = (layno == 0) ?
597 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
599 if (distoachieved < distotarget) {
601 stable_thresh = thresh;
607 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
608 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
609 THRESH_CALC, p_manager)) {
610 /* TODO: what to do with l ??? seek / tell ??? */
611 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
617 stable_thresh = thresh;
621 goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
625 /* Special value to indicate to use all passes */
629 if (cstr_info) { /* Threshold for Marcela Index */
630 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
633 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
636 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
637 (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
643 OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
644 opj_image_t * p_image,
646 opj_thread_pool_t* p_tp)
648 p_tcd->image = p_image;
651 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
652 sizeof(opj_tcd_tile_t));
653 if (! p_tcd->tcd_image->tiles) {
657 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
658 p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
659 if (! p_tcd->tcd_image->tiles->comps) {
663 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
664 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
665 p_tcd->thread_pool = p_tp;
671 Destroy a previously created TCD handle
673 void opj_tcd_destroy(opj_tcd_t *tcd)
676 opj_tcd_free_tile(tcd);
678 if (tcd->tcd_image) {
679 opj_free(tcd->tcd_image);
683 opj_free(tcd->used_component);
689 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
691 if ((l_tilec->data == 00) ||
692 ((l_tilec->data_size_needed > l_tilec->data_size) &&
693 (l_tilec->ownsData == OPJ_FALSE))) {
694 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
695 if (!l_tilec->data && l_tilec->data_size_needed != 0) {
698 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
699 l_tilec->data_size = l_tilec->data_size_needed;
700 l_tilec->ownsData = OPJ_TRUE;
701 } else if (l_tilec->data_size_needed > l_tilec->data_size) {
702 /* We don't need to keep old data */
703 opj_image_data_free(l_tilec->data);
704 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
705 if (! l_tilec->data) {
706 l_tilec->data_size = 0;
707 l_tilec->data_size_needed = 0;
708 l_tilec->ownsData = OPJ_FALSE;
711 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
712 l_tilec->data_size = l_tilec->data_size_needed;
713 l_tilec->ownsData = OPJ_TRUE;
718 /* ----------------------------------------------------------------------- */
720 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
721 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
722 opj_event_mgr_t* manager)
724 OPJ_UINT32(*l_gain_ptr)(OPJ_UINT32) = 00;
725 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
726 opj_tcp_t * l_tcp = 00;
727 opj_cp_t * l_cp = 00;
728 opj_tcd_tile_t * l_tile = 00;
729 opj_tccp_t *l_tccp = 00;
730 opj_tcd_tilecomp_t *l_tilec = 00;
731 opj_image_comp_t * l_image_comp = 00;
732 opj_tcd_resolution_t *l_res = 00;
733 opj_tcd_band_t *l_band = 00;
734 opj_stepsize_t * l_step_size = 00;
735 opj_tcd_precinct_t *l_current_precinct = 00;
736 opj_image_t *l_image = 00;
738 OPJ_UINT32 l_level_no;
739 OPJ_UINT32 l_pdx, l_pdy;
741 OPJ_INT32 l_x0b, l_y0b;
742 OPJ_UINT32 l_tx0, l_ty0;
743 /* extent of precincts , top left, bottom right**/
744 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
745 /* number of precinct for a resolution */
746 OPJ_UINT32 l_nb_precincts;
747 /* room needed to store l_nb_precinct precinct for a resolution */
748 OPJ_UINT32 l_nb_precinct_size;
749 /* number of code blocks for a precinct*/
750 OPJ_UINT32 l_nb_code_blocks;
751 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
752 OPJ_UINT32 l_nb_code_blocks_size;
753 /* size of data for a tile */
754 OPJ_UINT32 l_data_size;
757 l_tcp = &(l_cp->tcps[p_tile_no]);
758 l_tile = p_tcd->tcd_image->tiles;
759 l_tccp = l_tcp->tccps;
760 l_tilec = l_tile->comps;
761 l_image = p_tcd->image;
762 l_image_comp = p_tcd->image->comps;
764 p = p_tile_no % l_cp->tw; /* tile coordinates */
765 q = p_tile_no / l_cp->tw;
766 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
768 /* 4 borders of the tile rescale on the image if necessary */
769 l_tx0 = l_cp->tx0 + p *
770 l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
771 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
772 l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
774 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
775 if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
776 opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
779 l_ty0 = l_cp->ty0 + q *
780 l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
781 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
782 l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
784 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
785 if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
786 opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
791 /* testcase 1888.pdf.asan.35.988 */
792 if (l_tccp->numresolutions == 0) {
793 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
796 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
798 /*tile->numcomps = image->numcomps; */
799 for (compno = 0; compno < l_tile->numcomps; ++compno) {
800 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
801 l_image_comp->resno_decoded = 0;
802 /* border of each l_tile component (global) */
803 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
804 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
805 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
806 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
807 l_tilec->compno = compno;
808 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
810 l_tilec->numresolutions = l_tccp->numresolutions;
811 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
812 l_tilec->minimum_num_resolutions = 1;
814 l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
815 l_cp->m_specific_param.m_dec.m_reduce;
819 OPJ_SIZE_T l_tile_data_size;
821 /* compute l_data_size with overflow check */
822 OPJ_SIZE_T w = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0);
823 OPJ_SIZE_T h = (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
825 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
826 if (h > 0 && w > SIZE_MAX / h) {
827 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
830 l_tile_data_size = w * h;
832 if (SIZE_MAX / sizeof(OPJ_UINT32) < l_tile_data_size) {
833 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
836 l_tile_data_size = l_tile_data_size * sizeof(OPJ_UINT32);
838 l_tilec->data_size_needed = l_tile_data_size;
841 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
842 opj_tcd_resolution_t);
844 opj_image_data_free(l_tilec->data_win);
845 l_tilec->data_win = NULL;
851 if (l_tilec->resolutions == 00) {
852 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
853 if (! l_tilec->resolutions) {
856 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
857 l_tilec->resolutions_size = l_data_size;
858 memset(l_tilec->resolutions, 0, l_data_size);
859 } else if (l_data_size > l_tilec->resolutions_size) {
860 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
861 l_tilec->resolutions, l_data_size);
862 if (! new_resolutions) {
863 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
864 opj_free(l_tilec->resolutions);
865 l_tilec->resolutions = NULL;
866 l_tilec->resolutions_size = 0;
869 l_tilec->resolutions = new_resolutions;
870 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
871 memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
872 l_data_size - l_tilec->resolutions_size);
873 l_tilec->resolutions_size = l_data_size;
876 l_level_no = l_tilec->numresolutions;
877 l_res = l_tilec->resolutions;
878 l_step_size = l_tccp->stepsizes;
879 if (l_tccp->qmfbid == 0) {
880 l_gain_ptr = &opj_dwt_getgain_real;
882 l_gain_ptr = &opj_dwt_getgain;
884 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
886 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
887 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
888 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
889 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
890 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
894 /* border for each resolution level (global) */
895 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
896 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
897 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
898 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
900 /*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);*/
901 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
902 l_pdx = l_tccp->prcw[resno];
903 l_pdy = l_tccp->prch[resno];
904 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
905 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
906 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
907 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
908 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
909 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
910 /*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 );*/
912 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
913 l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
914 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
915 l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
916 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
918 if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
919 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
922 l_nb_precincts = l_res->pw * l_res->ph;
924 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
926 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
929 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
932 tlcbgxstart = l_tl_prc_x_start;
933 tlcbgystart = l_tl_prc_y_start;
934 /*brcbgxend = l_br_prc_x_end;*/
935 /* brcbgyend = l_br_prc_y_end;*/
936 cbgwidthexpn = l_pdx;
937 cbgheightexpn = l_pdy;
940 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
941 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
942 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
943 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
944 cbgwidthexpn = l_pdx - 1;
945 cbgheightexpn = l_pdy - 1;
949 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
950 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
951 l_band = l_res->bands;
953 for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
955 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
959 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
960 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
961 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
962 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
964 l_band->bandno = bandno + 1;
965 /* x0b = 1 if bandno = 1 or 3 */
966 l_x0b = l_band->bandno & 1;
967 /* y0b = 1 if bandno = 2 or 3 */
968 l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
969 /* l_band border (global) */
970 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
971 l_level_no), (OPJ_INT32)(l_level_no + 1));
972 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
973 l_level_no), (OPJ_INT32)(l_level_no + 1));
974 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
975 l_level_no), (OPJ_INT32)(l_level_no + 1));
976 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
977 l_level_no), (OPJ_INT32)(l_level_no + 1));
981 /* Skip empty bands */
982 if (opj_tcd_is_band_empty(l_band)) {
983 /* Do not zero l_band->precints to avoid leaks */
984 /* but make sure we don't use it later, since */
985 /* it will point to precincts of previous bands... */
990 /** avoid an if with storing function pointer */
991 l_gain = (*l_gain_ptr)(l_band->bandno);
992 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
993 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
994 (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
995 /* Mb value of Equation E-2 in "E.1 Inverse quantization
996 * procedure" of the standard */
997 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
1000 if (!l_band->precincts && (l_nb_precincts > 0U)) {
1001 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
1002 l_nb_precinct_size);
1003 if (! l_band->precincts) {
1004 opj_event_msg(manager, EVT_ERROR,
1005 "Not enough memory to handle band precints\n");
1008 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
1009 memset(l_band->precincts, 0, l_nb_precinct_size);
1010 l_band->precincts_data_size = l_nb_precinct_size;
1011 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
1013 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
1014 l_band->precincts,/*3 * */ l_nb_precinct_size);
1015 if (! new_precincts) {
1016 opj_event_msg(manager, EVT_ERROR,
1017 "Not enough memory to handle band precints\n");
1018 opj_free(l_band->precincts);
1019 l_band->precincts = NULL;
1020 l_band->precincts_data_size = 0;
1023 l_band->precincts = new_precincts;
1024 /*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);*/
1025 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
1026 l_nb_precinct_size - l_band->precincts_data_size);
1027 l_band->precincts_data_size = l_nb_precinct_size;
1030 l_current_precinct = l_band->precincts;
1031 for (precno = 0; precno < l_nb_precincts; ++precno) {
1032 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1033 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1034 (1 << cbgwidthexpn);
1035 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1036 (1 << cbgheightexpn);
1037 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1038 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1039 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1040 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1042 /* precinct size (global) */
1043 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1045 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1046 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1047 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1048 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1049 /*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);*/
1051 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1052 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1053 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1054 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1055 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1056 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1057 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1058 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1059 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1060 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1061 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1062 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1063 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1065 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1068 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1069 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
1070 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof_block) <
1072 opj_event_msg(manager, EVT_ERROR,
1073 "Size of code block data exceeds system limits\n");
1076 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1078 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1079 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1080 if (! l_current_precinct->cblks.blocks) {
1083 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1085 memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1087 l_current_precinct->block_size = l_nb_code_blocks_size;
1088 } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1089 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1090 l_nb_code_blocks_size);
1092 opj_free(l_current_precinct->cblks.blocks);
1093 l_current_precinct->cblks.blocks = NULL;
1094 l_current_precinct->block_size = 0;
1095 opj_event_msg(manager, EVT_ERROR,
1096 "Not enough memory for current precinct codeblock element\n");
1099 l_current_precinct->cblks.blocks = new_blocks;
1100 /*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); */
1102 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1103 l_current_precinct->block_size
1105 , l_nb_code_blocks_size - l_current_precinct->block_size);
1107 l_current_precinct->block_size = l_nb_code_blocks_size;
1110 if (! l_current_precinct->incltree) {
1111 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1112 l_current_precinct->ch, manager);
1114 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1115 l_current_precinct->cw, l_current_precinct->ch, manager);
1118 if (! l_current_precinct->imsbtree) {
1119 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1120 l_current_precinct->ch, manager);
1122 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1123 l_current_precinct->cw, l_current_precinct->ch, manager);
1126 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1127 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1128 l_current_precinct->cw) * (1 << cblkwidthexpn);
1129 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1130 l_current_precinct->cw) * (1 << cblkheightexpn);
1131 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1132 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1135 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1137 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1140 /* code-block size (global) */
1141 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1142 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1143 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1144 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1146 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1150 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1152 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1155 /* code-block size (global) */
1156 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1157 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1158 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1159 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1162 ++l_current_precinct;
1174 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1175 opj_event_mgr_t* p_manager)
1177 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1178 sizeof(opj_tcd_cblk_enc_t), p_manager);
1181 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1182 opj_event_mgr_t* p_manager)
1184 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1185 sizeof(opj_tcd_cblk_dec_t), p_manager);
1189 * Allocates memory for an encoding code block (but not data memory).
1191 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1194 if (! p_code_block->layers) {
1195 /* no memset since data */
1196 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1197 sizeof(opj_tcd_layer_t));
1198 if (! p_code_block->layers) {
1202 if (! p_code_block->passes) {
1203 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1204 sizeof(opj_tcd_pass_t));
1205 if (! p_code_block->passes) {
1213 * Allocates data memory for an encoding code block.
1215 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1218 OPJ_UINT32 l_data_size;
1220 /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1221 /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
1222 /* TODO: is there a theoretical upper-bound for the compressed code */
1224 l_data_size = 2 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1225 (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1227 if (l_data_size > p_code_block->data_size) {
1228 if (p_code_block->data) {
1229 /* We refer to data - 1 since below we incremented it */
1230 opj_free(p_code_block->data - 1);
1232 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1233 if (! p_code_block->data) {
1234 p_code_block->data_size = 0U;
1237 p_code_block->data_size = l_data_size;
1239 /* We reserve the initial byte as a fake byte to a non-FF value */
1240 /* and increment the data pointer, so that opj_mqc_init_enc() */
1241 /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1243 p_code_block->data[0] = 0;
1244 p_code_block->data += 1; /*why +1 ?*/
1250 void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1252 memset(seg, 0, sizeof(opj_tcd_seg_t));
1256 * Allocates memory for a decoding code block.
1258 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1261 if (! p_code_block->segs) {
1263 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1264 sizeof(opj_tcd_seg_t));
1265 if (! p_code_block->segs) {
1268 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1270 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1271 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1274 opj_tcd_seg_t * l_segs = p_code_block->segs;
1275 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1276 opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
1277 OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
1280 opj_aligned_free(p_code_block->decoded_data);
1281 p_code_block->decoded_data = 00;
1283 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1284 p_code_block->segs = l_segs;
1285 p_code_block->m_current_max_segs = l_current_max_segs;
1286 for (i = 0; i < l_current_max_segs; ++i) {
1287 opj_tcd_reinit_segment(&l_segs[i]);
1289 p_code_block->chunks = l_chunks;
1290 p_code_block->numchunksalloc = l_numchunksalloc;
1296 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd,
1297 OPJ_BOOL take_into_account_partial_decoding)
1300 OPJ_UINT32 l_data_size = 0;
1301 opj_image_comp_t * l_img_comp = 00;
1302 opj_tcd_tilecomp_t * l_tile_comp = 00;
1303 opj_tcd_resolution_t * l_res = 00;
1304 OPJ_UINT32 l_size_comp, l_remaining;
1307 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1308 l_img_comp = p_tcd->image->comps;
1310 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1312 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1313 l_remaining = l_img_comp->prec & 7; /* (%8) */
1319 if (l_size_comp == 3) {
1323 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1324 if (take_into_account_partial_decoding && !p_tcd->whole_tile_decoding) {
1325 w = l_res->win_x1 - l_res->win_x0;
1326 h = l_res->win_y1 - l_res->win_y0;
1328 w = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1329 h = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1331 if (h > 0 && UINT_MAX / w < h) {
1335 if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1338 l_temp *= l_size_comp;
1340 if (l_temp > UINT_MAX - l_data_size) {
1343 l_data_size += l_temp;
1351 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1352 OPJ_UINT32 p_tile_no,
1354 OPJ_UINT32 * p_data_written,
1355 OPJ_UINT32 p_max_length,
1356 opj_codestream_info_t *p_cstr_info,
1357 opj_event_mgr_t *p_manager)
1360 if (p_tcd->cur_tp_num == 0) {
1362 p_tcd->tcd_tileno = p_tile_no;
1363 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1365 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1367 OPJ_UINT32 l_num_packs = 0;
1369 opj_tcd_tilecomp_t *l_tilec_idx =
1370 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1371 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1373 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1374 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1376 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1377 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1379 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1380 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1381 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1383 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1384 OPJ_SIZE_T)p_cstr_info->numcomps * (OPJ_SIZE_T)p_cstr_info->numlayers *
1386 sizeof(opj_packet_info_t));
1387 if (!p_cstr_info->tile[p_tile_no].packet) {
1388 /* FIXME event manager error callback */
1394 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1395 /*---------------TILE-------------------*/
1396 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1399 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1401 /* FIXME _ProfStart(PGROUP_MCT); */
1402 if (! opj_tcd_mct_encode(p_tcd)) {
1405 /* FIXME _ProfStop(PGROUP_MCT); */
1407 /* FIXME _ProfStart(PGROUP_DWT); */
1408 if (! opj_tcd_dwt_encode(p_tcd)) {
1411 /* FIXME _ProfStop(PGROUP_DWT); */
1413 /* FIXME _ProfStart(PGROUP_T1); */
1414 if (! opj_tcd_t1_encode(p_tcd)) {
1417 /* FIXME _ProfStop(PGROUP_T1); */
1419 /* FIXME _ProfStart(PGROUP_RATE); */
1420 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
1421 p_cstr_info, p_manager)) {
1424 /* FIXME _ProfStop(PGROUP_RATE); */
1427 /*--------------TIER2------------------*/
1431 p_cstr_info->index_write = 1;
1433 /* FIXME _ProfStart(PGROUP_T2); */
1435 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1436 p_cstr_info, p_manager)) {
1439 /* FIXME _ProfStop(PGROUP_T2); */
1441 /*---------------CLEAN-------------------*/
1446 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1451 OPJ_UINT32 numcomps_to_decode,
1452 const OPJ_UINT32 *comps_indices,
1454 OPJ_UINT32 p_max_length,
1455 OPJ_UINT32 p_tile_no,
1456 opj_codestream_index_t *p_cstr_index,
1457 opj_event_mgr_t *p_manager
1460 OPJ_UINT32 l_data_read;
1463 p_tcd->tcd_tileno = p_tile_no;
1464 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1465 p_tcd->win_x0 = win_x0;
1466 p_tcd->win_y0 = win_y0;
1467 p_tcd->win_x1 = win_x1;
1468 p_tcd->win_y1 = win_y1;
1469 p_tcd->whole_tile_decoding = OPJ_TRUE;
1471 opj_free(p_tcd->used_component);
1472 p_tcd->used_component = NULL;
1474 if (numcomps_to_decode) {
1475 OPJ_BOOL* used_component = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
1476 p_tcd->image->numcomps);
1477 if (used_component == NULL) {
1480 for (compno = 0; compno < numcomps_to_decode; compno++) {
1481 used_component[ comps_indices[compno] ] = OPJ_TRUE;
1484 p_tcd->used_component = used_component;
1487 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1488 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1492 if (!opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno)) {
1493 p_tcd->whole_tile_decoding = OPJ_FALSE;
1498 if (p_tcd->whole_tile_decoding) {
1499 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1500 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1501 opj_tcd_resolution_t *l_res = &
1502 (tilec->resolutions[tilec->minimum_num_resolutions - 1]);
1503 OPJ_SIZE_T l_data_size;
1505 /* compute l_data_size with overflow check */
1506 OPJ_SIZE_T res_w = (OPJ_SIZE_T)(l_res->x1 - l_res->x0);
1507 OPJ_SIZE_T res_h = (OPJ_SIZE_T)(l_res->y1 - l_res->y0);
1509 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1513 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
1514 if (res_h > 0 && res_w > SIZE_MAX / res_h) {
1515 opj_event_msg(p_manager, EVT_ERROR,
1516 "Size of tile data exceeds system limits\n");
1519 l_data_size = res_w * res_h;
1521 if (SIZE_MAX / sizeof(OPJ_UINT32) < l_data_size) {
1522 opj_event_msg(p_manager, EVT_ERROR,
1523 "Size of tile data exceeds system limits\n");
1526 l_data_size *= sizeof(OPJ_UINT32);
1528 tilec->data_size_needed = l_data_size;
1530 if (!opj_alloc_tile_component_data(tilec)) {
1531 opj_event_msg(p_manager, EVT_ERROR,
1532 "Size of tile data exceeds system limits\n");
1537 /* Compute restricted tile-component and tile-resolution coordinates */
1538 /* of the window of interest, but defer the memory allocation until */
1539 /* we know the resno_decoded */
1540 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1542 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1543 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1545 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1549 /* Compute the intersection of the area of interest, expressed in tile coordinates */
1550 /* with the tile coordinates */
1551 tilec->win_x0 = opj_uint_max(
1552 (OPJ_UINT32)tilec->x0,
1553 opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
1554 tilec->win_y0 = opj_uint_max(
1555 (OPJ_UINT32)tilec->y0,
1556 opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
1557 tilec->win_x1 = opj_uint_min(
1558 (OPJ_UINT32)tilec->x1,
1559 opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
1560 tilec->win_y1 = opj_uint_min(
1561 (OPJ_UINT32)tilec->y1,
1562 opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
1563 if (tilec->win_x1 < tilec->win_x0 ||
1564 tilec->win_y1 < tilec->win_y0) {
1565 /* We should not normally go there. The circumstance is when */
1566 /* the tile coordinates do not intersect the area of interest */
1567 /* Upper level logic should not even try to decode that tile */
1568 opj_event_msg(p_manager, EVT_ERROR,
1569 "Invalid tilec->win_xxx values\n");
1573 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1574 opj_tcd_resolution_t *res = tilec->resolutions + resno;
1575 res->win_x0 = opj_uint_ceildivpow2(tilec->win_x0,
1576 tilec->numresolutions - 1 - resno);
1577 res->win_y0 = opj_uint_ceildivpow2(tilec->win_y0,
1578 tilec->numresolutions - 1 - resno);
1579 res->win_x1 = opj_uint_ceildivpow2(tilec->win_x1,
1580 tilec->numresolutions - 1 - resno);
1581 res->win_y1 = opj_uint_ceildivpow2(tilec->win_y1,
1582 tilec->numresolutions - 1 - resno);
1587 #ifdef TODO_MSD /* FIXME */
1590 OPJ_UINT32 resno, compno, numprec = 0;
1591 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1592 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1593 opj_tccp_t *tccp = &tcp->tccps[compno];
1594 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1595 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1596 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1597 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1598 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1599 numprec += res_idx->pw * res_idx->ph;
1600 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1601 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1604 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1605 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1606 p_cstr_info->packno = 0;
1611 /*--------------TIER2------------------*/
1612 /* FIXME _ProfStart(PGROUP_T2); */
1614 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1618 /* FIXME _ProfStop(PGROUP_T2); */
1620 /*------------------TIER1-----------------*/
1622 /* FIXME _ProfStart(PGROUP_T1); */
1623 if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1626 /* FIXME _ProfStop(PGROUP_T1); */
1629 /* For subtile decoding, now we know the resno_decoded, we can allocate */
1630 /* the tile data buffer */
1631 if (!p_tcd->whole_tile_decoding) {
1632 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1633 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1634 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1635 opj_tcd_resolution_t *res = tilec->resolutions + image_comp->resno_decoded;
1636 OPJ_SIZE_T w = res->win_x1 - res->win_x0;
1637 OPJ_SIZE_T h = res->win_y1 - res->win_y0;
1638 OPJ_SIZE_T l_data_size;
1640 opj_image_data_free(tilec->data_win);
1641 tilec->data_win = NULL;
1643 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1647 if (w > 0 && h > 0) {
1648 if (w > SIZE_MAX / h) {
1649 opj_event_msg(p_manager, EVT_ERROR,
1650 "Size of tile data exceeds system limits\n");
1653 l_data_size = w * h;
1654 if (l_data_size > SIZE_MAX / sizeof(OPJ_INT32)) {
1655 opj_event_msg(p_manager, EVT_ERROR,
1656 "Size of tile data exceeds system limits\n");
1659 l_data_size *= sizeof(OPJ_INT32);
1661 tilec->data_win = (OPJ_INT32*) opj_image_data_alloc(l_data_size);
1662 if (tilec->data_win == NULL) {
1663 opj_event_msg(p_manager, EVT_ERROR,
1664 "Size of tile data exceeds system limits\n");
1671 /*----------------DWT---------------------*/
1673 /* FIXME _ProfStart(PGROUP_DWT); */
1675 (! opj_tcd_dwt_decode(p_tcd)) {
1678 /* FIXME _ProfStop(PGROUP_DWT); */
1680 /*----------------MCT-------------------*/
1681 /* FIXME _ProfStart(PGROUP_MCT); */
1683 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1686 /* FIXME _ProfStop(PGROUP_MCT); */
1688 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1690 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1693 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1696 /*---------------TILE-------------------*/
1700 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1702 OPJ_UINT32 p_dest_length
1705 OPJ_UINT32 i, j, k, l_data_size = 0;
1706 opj_image_comp_t * l_img_comp = 00;
1707 opj_tcd_tilecomp_t * l_tilec = 00;
1708 opj_tcd_resolution_t * l_res;
1709 OPJ_UINT32 l_size_comp, l_remaining;
1710 OPJ_UINT32 l_stride, l_width, l_height;
1712 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, OPJ_TRUE);
1713 if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1717 l_tilec = p_tcd->tcd_image->tiles->comps;
1718 l_img_comp = p_tcd->image->comps;
1720 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1721 const OPJ_INT32* l_src_data;
1722 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1723 l_remaining = l_img_comp->prec & 7; /* (%8) */
1724 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1725 if (p_tcd->whole_tile_decoding) {
1726 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1727 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1728 l_stride = (OPJ_UINT32)(l_tilec->resolutions[l_tilec->minimum_num_resolutions -
1730 l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0) - l_width;
1731 l_src_data = l_tilec->data;
1733 l_width = l_res->win_x1 - l_res->win_x0;
1734 l_height = l_res->win_y1 - l_res->win_y0;
1736 l_src_data = l_tilec->data_win;
1743 if (l_size_comp == 3) {
1747 switch (l_size_comp) {
1749 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1750 const OPJ_INT32 * l_src_ptr = l_src_data;
1752 if (l_img_comp->sgnd) {
1753 for (j = 0; j < l_height; ++j) {
1754 for (k = 0; k < l_width; ++k) {
1755 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1757 l_src_ptr += l_stride;
1760 for (j = 0; j < l_height; ++j) {
1761 for (k = 0; k < l_width; ++k) {
1762 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1764 l_src_ptr += l_stride;
1768 p_dest = (OPJ_BYTE *)l_dest_ptr;
1772 const OPJ_INT32 * l_src_ptr = l_src_data;
1773 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1775 if (l_img_comp->sgnd) {
1776 for (j = 0; j < l_height; ++j) {
1777 for (k = 0; k < l_width; ++k) {
1778 OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1779 memcpy(l_dest_ptr, &val, sizeof(val));
1782 l_src_ptr += l_stride;
1785 for (j = 0; j < l_height; ++j) {
1786 for (k = 0; k < l_width; ++k) {
1787 OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1788 memcpy(l_dest_ptr, &val, sizeof(val));
1791 l_src_ptr += l_stride;
1795 p_dest = (OPJ_BYTE*) l_dest_ptr;
1799 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1800 const OPJ_INT32 * l_src_ptr = l_src_data;
1802 for (j = 0; j < l_height; ++j) {
1803 memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1804 l_dest_ptr += l_width;
1805 l_src_ptr += l_width + l_stride;
1808 p_dest = (OPJ_BYTE*) l_dest_ptr;
1823 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1825 OPJ_UINT32 compno, resno, bandno, precno;
1826 opj_tcd_tile_t *l_tile = 00;
1827 opj_tcd_tilecomp_t *l_tile_comp = 00;
1828 opj_tcd_resolution_t *l_res = 00;
1829 opj_tcd_band_t *l_band = 00;
1830 opj_tcd_precinct_t *l_precinct = 00;
1831 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1832 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1838 if (! p_tcd->tcd_image) {
1842 if (p_tcd->m_is_decoder) {
1843 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1845 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1848 l_tile = p_tcd->tcd_image->tiles;
1853 l_tile_comp = l_tile->comps;
1855 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1856 l_res = l_tile_comp->resolutions;
1859 l_nb_resolutions = l_tile_comp->resolutions_size / (OPJ_UINT32)sizeof(
1860 opj_tcd_resolution_t);
1861 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1862 l_band = l_res->bands;
1863 for (bandno = 0; bandno < 3; ++bandno) {
1864 l_precinct = l_band->precincts;
1867 l_nb_precincts = l_band->precincts_data_size / (OPJ_UINT32)sizeof(
1868 opj_tcd_precinct_t);
1869 for (precno = 0; precno < l_nb_precincts; ++precno) {
1870 opj_tgt_destroy(l_precinct->incltree);
1871 l_precinct->incltree = 00;
1872 opj_tgt_destroy(l_precinct->imsbtree);
1873 l_precinct->imsbtree = 00;
1874 (*l_tcd_code_block_deallocate)(l_precinct);
1878 opj_free(l_band->precincts);
1879 l_band->precincts = 00;
1886 opj_free(l_tile_comp->resolutions);
1887 l_tile_comp->resolutions = 00;
1890 if (l_tile_comp->ownsData && l_tile_comp->data) {
1891 opj_image_data_free(l_tile_comp->data);
1892 l_tile_comp->data = 00;
1893 l_tile_comp->ownsData = 0;
1894 l_tile_comp->data_size = 0;
1895 l_tile_comp->data_size_needed = 0;
1898 opj_image_data_free(l_tile_comp->data_win);
1903 opj_free(l_tile->comps);
1905 opj_free(p_tcd->tcd_image->tiles);
1906 p_tcd->tcd_image->tiles = 00;
1910 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1911 OPJ_BYTE * p_src_data,
1912 OPJ_UINT32 * p_data_read,
1913 OPJ_UINT32 p_max_src_size,
1914 opj_codestream_index_t *p_cstr_index,
1915 opj_event_mgr_t *p_manager
1920 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1925 if (! opj_t2_decode_packets(
1929 p_tcd->tcd_image->tiles,
1935 opj_t2_destroy(l_t2);
1939 opj_t2_destroy(l_t2);
1941 /*---------------CLEAN-------------------*/
1945 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1948 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1949 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1950 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1951 volatile OPJ_BOOL ret = OPJ_TRUE;
1952 OPJ_BOOL check_pterm = OPJ_FALSE;
1953 opj_mutex_t* p_manager_mutex = NULL;
1955 p_manager_mutex = opj_mutex_create();
1957 /* Only enable PTERM check if we decode all layers */
1958 if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
1959 (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
1960 check_pterm = OPJ_TRUE;
1963 for (compno = 0; compno < l_tile->numcomps;
1964 ++compno, ++l_tile_comp, ++l_tccp) {
1965 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1969 opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp,
1970 p_manager, p_manager_mutex, check_pterm);
1976 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1977 if (p_manager_mutex) {
1978 opj_mutex_destroy(p_manager_mutex);
1984 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1987 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1988 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1989 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1990 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1992 for (compno = 0; compno < l_tile->numcomps;
1993 compno++, ++l_tile_comp, ++l_img_comp, ++l_tccp) {
1994 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1998 if (l_tccp->qmfbid == 1) {
1999 if (! opj_dwt_decode(p_tcd, l_tile_comp,
2000 l_img_comp->resno_decoded + 1)) {
2004 if (! opj_dwt_decode_real(p_tcd, l_tile_comp,
2005 l_img_comp->resno_decoded + 1)) {
2015 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
2017 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2018 opj_tcp_t * l_tcp = p_tcd->tcp;
2019 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2020 OPJ_SIZE_T l_samples;
2023 if (l_tcp->mct == 0 || p_tcd->used_component != NULL) {
2027 if (p_tcd->whole_tile_decoding) {
2028 opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2029 l_tile_comp->minimum_num_resolutions - 1;
2031 /* A bit inefficient: we process more data than needed if */
2032 /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */
2033 /* but we would need to take into account a stride then */
2034 l_samples = (OPJ_SIZE_T)(res_comp0->x1 - res_comp0->x0) *
2035 (OPJ_SIZE_T)(res_comp0->y1 - res_comp0->y0);
2036 if (l_tile->numcomps >= 3) {
2037 if (l_tile_comp->minimum_num_resolutions !=
2038 l_tile->comps[1].minimum_num_resolutions ||
2039 l_tile_comp->minimum_num_resolutions !=
2040 l_tile->comps[2].minimum_num_resolutions) {
2041 opj_event_msg(p_manager, EVT_ERROR,
2042 "Tiles don't all have the same dimension. Skip the MCT step.\n");
2046 if (l_tile->numcomps >= 3) {
2047 opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2048 l_tile_comp->minimum_num_resolutions - 1;
2049 opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2050 l_tile_comp->minimum_num_resolutions - 1;
2051 /* testcase 1336.pdf.asan.47.376 */
2052 if (p_tcd->image->comps[0].resno_decoded !=
2053 p_tcd->image->comps[1].resno_decoded ||
2054 p_tcd->image->comps[0].resno_decoded !=
2055 p_tcd->image->comps[2].resno_decoded ||
2056 (OPJ_SIZE_T)(res_comp1->x1 - res_comp1->x0) *
2057 (OPJ_SIZE_T)(res_comp1->y1 - res_comp1->y0) != l_samples ||
2058 (OPJ_SIZE_T)(res_comp2->x1 - res_comp2->x0) *
2059 (OPJ_SIZE_T)(res_comp2->y1 - res_comp2->y0) != l_samples) {
2060 opj_event_msg(p_manager, EVT_ERROR,
2061 "Tiles don't all have the same dimension. Skip the MCT step.\n");
2066 opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2067 p_tcd->image->comps[0].resno_decoded;
2069 l_samples = (OPJ_SIZE_T)(res_comp0->win_x1 - res_comp0->win_x0) *
2070 (OPJ_SIZE_T)(res_comp0->win_y1 - res_comp0->win_y0);
2071 if (l_tile->numcomps >= 3) {
2072 opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2073 p_tcd->image->comps[1].resno_decoded;
2074 opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2075 p_tcd->image->comps[2].resno_decoded;
2076 /* testcase 1336.pdf.asan.47.376 */
2077 if (p_tcd->image->comps[0].resno_decoded !=
2078 p_tcd->image->comps[1].resno_decoded ||
2079 p_tcd->image->comps[0].resno_decoded !=
2080 p_tcd->image->comps[2].resno_decoded ||
2081 (OPJ_SIZE_T)(res_comp1->win_x1 - res_comp1->win_x0) *
2082 (OPJ_SIZE_T)(res_comp1->win_y1 - res_comp1->win_y0) != l_samples ||
2083 (OPJ_SIZE_T)(res_comp2->win_x1 - res_comp2->win_x0) *
2084 (OPJ_SIZE_T)(res_comp2->win_y1 - res_comp2->win_y0) != l_samples) {
2085 opj_event_msg(p_manager, EVT_ERROR,
2086 "Tiles don't all have the same dimension. Skip the MCT step.\n");
2092 if (l_tile->numcomps >= 3) {
2093 if (l_tcp->mct == 2) {
2096 if (! l_tcp->m_mct_decoding_matrix) {
2100 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2105 for (i = 0; i < l_tile->numcomps; ++i) {
2106 if (p_tcd->whole_tile_decoding) {
2107 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2109 l_data[i] = (OPJ_BYTE*) l_tile_comp->data_win;
2114 if (! opj_mct_decode_custom(/* MCT data */
2115 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
2116 /* size of components */
2120 /* nb of components (i.e. size of pData) */
2122 /* tells if the data is signed */
2123 p_tcd->image->comps->sgnd)) {
2130 if (l_tcp->tccps->qmfbid == 1) {
2131 if (p_tcd->whole_tile_decoding) {
2132 opj_mct_decode(l_tile->comps[0].data,
2133 l_tile->comps[1].data,
2134 l_tile->comps[2].data,
2137 opj_mct_decode(l_tile->comps[0].data_win,
2138 l_tile->comps[1].data_win,
2139 l_tile->comps[2].data_win,
2143 if (p_tcd->whole_tile_decoding) {
2144 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
2145 (OPJ_FLOAT32*)l_tile->comps[1].data,
2146 (OPJ_FLOAT32*)l_tile->comps[2].data,
2149 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data_win,
2150 (OPJ_FLOAT32*)l_tile->comps[1].data_win,
2151 (OPJ_FLOAT32*)l_tile->comps[2].data_win,
2157 opj_event_msg(p_manager, EVT_ERROR,
2158 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
2166 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
2169 opj_tcd_tilecomp_t * l_tile_comp = 00;
2170 opj_tccp_t * l_tccp = 00;
2171 opj_image_comp_t * l_img_comp = 00;
2172 opj_tcd_resolution_t* l_res = 00;
2173 opj_tcd_tile_t * l_tile;
2174 OPJ_UINT32 l_width, l_height, i, j;
2175 OPJ_INT32 * l_current_ptr;
2176 OPJ_INT32 l_min, l_max;
2177 OPJ_UINT32 l_stride;
2179 l_tile = p_tcd->tcd_image->tiles;
2180 l_tile_comp = l_tile->comps;
2181 l_tccp = p_tcd->tcp->tccps;
2182 l_img_comp = p_tcd->image->comps;
2184 for (compno = 0; compno < l_tile->numcomps;
2185 compno++, ++l_img_comp, ++l_tccp, ++l_tile_comp) {
2187 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2191 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
2193 if (!p_tcd->whole_tile_decoding) {
2194 l_width = l_res->win_x1 - l_res->win_x0;
2195 l_height = l_res->win_y1 - l_res->win_y0;
2197 l_current_ptr = l_tile_comp->data_win;
2199 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
2200 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
2201 l_stride = (OPJ_UINT32)(
2202 l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x1 -
2203 l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x0)
2205 l_current_ptr = l_tile_comp->data;
2207 assert(l_height == 0 ||
2208 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
2211 if (l_img_comp->sgnd) {
2212 l_min = -(1 << (l_img_comp->prec - 1));
2213 l_max = (1 << (l_img_comp->prec - 1)) - 1;
2216 l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
2220 if (l_tccp->qmfbid == 1) {
2221 for (j = 0; j < l_height; ++j) {
2222 for (i = 0; i < l_width; ++i) {
2223 /* TODO: do addition on int64 ? */
2224 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
2228 l_current_ptr += l_stride;
2231 for (j = 0; j < l_height; ++j) {
2232 for (i = 0; i < l_width; ++i) {
2233 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
2234 if (l_value > INT_MAX) {
2235 *l_current_ptr = l_max;
2236 } else if (l_value < INT_MIN) {
2237 *l_current_ptr = l_min;
2239 /* Do addition on int64 to avoid overflows */
2240 OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value);
2241 *l_current_ptr = (OPJ_INT32)opj_int64_clamp(
2242 l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
2246 l_current_ptr += l_stride;
2257 * Deallocates the encoding data of the given precinct.
2259 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
2261 OPJ_UINT32 cblkno, l_nb_code_blocks;
2263 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
2265 /*fprintf(stderr,"deallocate codeblock:{\n");*/
2266 /*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);*/
2267 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
2268 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 );*/
2271 l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2272 opj_tcd_cblk_dec_t);
2273 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
2275 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2277 if (l_code_block->segs) {
2278 opj_free(l_code_block->segs);
2279 l_code_block->segs = 00;
2282 if (l_code_block->chunks) {
2283 opj_free(l_code_block->chunks);
2284 l_code_block->chunks = 00;
2287 opj_aligned_free(l_code_block->decoded_data);
2288 l_code_block->decoded_data = NULL;
2293 opj_free(p_precinct->cblks.dec);
2294 p_precinct->cblks.dec = 00;
2299 * Deallocates the encoding data of the given precinct.
2301 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
2303 OPJ_UINT32 cblkno, l_nb_code_blocks;
2305 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
2307 l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2308 opj_tcd_cblk_enc_t);
2310 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2311 if (l_code_block->data) {
2312 /* We refer to data - 1 since below we incremented it */
2313 /* in opj_tcd_code_block_enc_allocate_data() */
2314 opj_free(l_code_block->data - 1);
2315 l_code_block->data = 00;
2318 if (l_code_block->layers) {
2319 opj_free(l_code_block->layers);
2320 l_code_block->layers = 00;
2323 if (l_code_block->passes) {
2324 opj_free(l_code_block->passes);
2325 l_code_block->passes = 00;
2330 opj_free(p_precinct->cblks.enc);
2332 p_precinct->cblks.enc = 00;
2336 OPJ_SIZE_T opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
2339 OPJ_SIZE_T l_data_size = 0;
2340 opj_image_comp_t * l_img_comp = 00;
2341 opj_tcd_tilecomp_t * l_tilec = 00;
2342 OPJ_UINT32 l_size_comp, l_remaining;
2344 l_tilec = p_tcd->tcd_image->tiles->comps;
2345 l_img_comp = p_tcd->image->comps;
2346 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2347 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2348 l_remaining = l_img_comp->prec & 7; /* (%8) */
2354 if (l_size_comp == 3) {
2358 l_data_size += l_size_comp * ((OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2359 (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0));
2367 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2370 opj_tcd_tilecomp_t * l_tile_comp = 00;
2371 opj_tccp_t * l_tccp = 00;
2372 opj_image_comp_t * l_img_comp = 00;
2373 opj_tcd_tile_t * l_tile;
2374 OPJ_SIZE_T l_nb_elem, i;
2375 OPJ_INT32 * l_current_ptr;
2377 l_tile = p_tcd->tcd_image->tiles;
2378 l_tile_comp = l_tile->comps;
2379 l_tccp = p_tcd->tcp->tccps;
2380 l_img_comp = p_tcd->image->comps;
2382 for (compno = 0; compno < l_tile->numcomps; compno++) {
2383 l_current_ptr = l_tile_comp->data;
2384 l_nb_elem = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2385 (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2387 if (l_tccp->qmfbid == 1) {
2388 for (i = 0; i < l_nb_elem; ++i) {
2389 *l_current_ptr -= l_tccp->m_dc_level_shift ;
2393 for (i = 0; i < l_nb_elem; ++i) {
2394 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2407 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2409 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2410 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2411 OPJ_SIZE_T samples = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2412 (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2414 OPJ_BYTE ** l_data = 00;
2415 opj_tcp_t * l_tcp = p_tcd->tcp;
2417 if (!p_tcd->tcp->mct) {
2421 if (p_tcd->tcp->mct == 2) {
2422 if (! p_tcd->tcp->m_mct_coding_matrix) {
2426 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2431 for (i = 0; i < l_tile->numcomps; ++i) {
2432 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2436 if (! opj_mct_encode_custom(/* MCT data */
2437 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2438 /* size of components */
2442 /* nb of components (i.e. size of pData) */
2444 /* tells if the data is signed */
2445 p_tcd->image->comps->sgnd)) {
2451 } else if (l_tcp->tccps->qmfbid == 0) {
2452 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2453 l_tile->comps[2].data, samples);
2455 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2456 l_tile->comps[2].data, samples);
2462 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2464 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2465 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2466 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2469 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2470 if (l_tccp->qmfbid == 1) {
2471 if (! opj_dwt_encode(l_tile_comp)) {
2474 } else if (l_tccp->qmfbid == 0) {
2475 if (! opj_dwt_encode_real(l_tile_comp)) {
2487 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2490 const OPJ_FLOAT64 * l_mct_norms;
2491 OPJ_UINT32 l_mct_numcomps = 0U;
2492 opj_tcp_t * l_tcp = p_tcd->tcp;
2494 l_t1 = opj_t1_create(OPJ_TRUE);
2499 if (l_tcp->mct == 1) {
2500 l_mct_numcomps = 3U;
2501 /* irreversible encoding */
2502 if (l_tcp->tccps->qmfbid == 0) {
2503 l_mct_norms = opj_mct_get_mct_norms_real();
2505 l_mct_norms = opj_mct_get_mct_norms();
2508 l_mct_numcomps = p_tcd->image->numcomps;
2509 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2512 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2514 opj_t1_destroy(l_t1);
2518 opj_t1_destroy(l_t1);
2523 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2524 OPJ_BYTE * p_dest_data,
2525 OPJ_UINT32 * p_data_written,
2526 OPJ_UINT32 p_max_dest_size,
2527 opj_codestream_info_t *p_cstr_info,
2528 opj_event_mgr_t *p_manager)
2532 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2537 if (! opj_t2_encode_packets(
2540 p_tcd->tcd_image->tiles,
2541 p_tcd->tcp->numlayers,
2551 opj_t2_destroy(l_t2);
2555 opj_t2_destroy(l_t2);
2557 /*---------------CLEAN-------------------*/
2562 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2563 OPJ_BYTE * p_dest_data,
2564 OPJ_UINT32 p_max_dest_size,
2565 opj_codestream_info_t *p_cstr_info,
2566 opj_event_mgr_t *p_manager)
2568 opj_cp_t * l_cp = p_tcd->cp;
2569 OPJ_UINT32 l_nb_written = 0;
2572 p_cstr_info->index_write = 0;
2575 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2576 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2578 /* Normal Rate/distortion allocation */
2579 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2580 p_cstr_info, p_manager)) {
2584 /* Fixed layer allocation */
2585 opj_tcd_rateallocate_fixed(p_tcd);
2592 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2594 OPJ_SIZE_T p_src_length)
2598 OPJ_SIZE_T l_data_size = 0;
2599 opj_image_comp_t * l_img_comp = 00;
2600 opj_tcd_tilecomp_t * l_tilec = 00;
2601 OPJ_UINT32 l_size_comp, l_remaining;
2602 OPJ_SIZE_T l_nb_elem;
2604 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2605 if (l_data_size != p_src_length) {
2609 l_tilec = p_tcd->tcd_image->tiles->comps;
2610 l_img_comp = p_tcd->image->comps;
2611 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2612 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2613 l_remaining = l_img_comp->prec & 7; /* (%8) */
2614 l_nb_elem = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2615 (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
2621 if (l_size_comp == 3) {
2625 switch (l_size_comp) {
2627 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2628 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2630 if (l_img_comp->sgnd) {
2631 for (j = 0; j < l_nb_elem; ++j) {
2632 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2635 for (j = 0; j < l_nb_elem; ++j) {
2636 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2640 p_src = (OPJ_BYTE*) l_src_ptr;
2644 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2645 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2647 if (l_img_comp->sgnd) {
2648 for (j = 0; j < l_nb_elem; ++j) {
2649 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2652 for (j = 0; j < l_nb_elem; ++j) {
2653 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2657 p_src = (OPJ_BYTE*) l_src_ptr;
2661 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2662 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2664 for (j = 0; j < l_nb_elem; ++j) {
2665 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2668 p_src = (OPJ_BYTE*) l_src_ptr;
2680 OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2682 return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);
2685 OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd,
2694 /* Note: those values for filter_margin are in part the result of */
2695 /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
2696 /* to the maximum left/right extension given in tables F.2 and F.3 of the */
2697 /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
2698 /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
2699 /* needed to be bumped to 4, in case inconsistencies are found while */
2700 /* decoding parts of irreversible coded images. */
2701 /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
2702 OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3;
2703 opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]);
2704 opj_image_comp_t* image_comp = &(tcd->image->comps[compno]);
2705 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2706 /* with the tile coordinates */
2707 OPJ_UINT32 tcx0 = opj_uint_max(
2708 (OPJ_UINT32)tilec->x0,
2709 opj_uint_ceildiv(tcd->win_x0, image_comp->dx));
2710 OPJ_UINT32 tcy0 = opj_uint_max(
2711 (OPJ_UINT32)tilec->y0,
2712 opj_uint_ceildiv(tcd->win_y0, image_comp->dy));
2713 OPJ_UINT32 tcx1 = opj_uint_min(
2714 (OPJ_UINT32)tilec->x1,
2715 opj_uint_ceildiv(tcd->win_x1, image_comp->dx));
2716 OPJ_UINT32 tcy1 = opj_uint_min(
2717 (OPJ_UINT32)tilec->y1,
2718 opj_uint_ceildiv(tcd->win_y1, image_comp->dy));
2719 /* Compute number of decomposition for this band. See table F-1 */
2720 OPJ_UINT32 nb = (resno == 0) ?
2721 tilec->numresolutions - 1 :
2722 tilec->numresolutions - resno;
2723 /* Map above tile-based coordinates to sub-band-based coordinates per */
2724 /* equation B-15 of the standard */
2725 OPJ_UINT32 x0b = bandno & 1;
2726 OPJ_UINT32 y0b = bandno >> 1;
2727 OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 :
2728 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2729 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2730 OPJ_UINT32 tby0 = (nb == 0) ? tcy0 :
2731 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2732 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2733 OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 :
2734 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2735 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2736 OPJ_UINT32 tby1 = (nb == 0) ? tcy1 :
2737 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2738 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2739 OPJ_BOOL intersects;
2741 if (tbx0 < filter_margin) {
2744 tbx0 -= filter_margin;
2746 if (tby0 < filter_margin) {
2749 tby0 -= filter_margin;
2751 tbx1 = opj_uint_adds(tbx1, filter_margin);
2752 tby1 = opj_uint_adds(tby1, filter_margin);
2754 intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 &&
2757 #ifdef DEBUG_VERBOSE
2758 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",
2759 compno, resno, nb, bandno, x0b, y0b,
2760 band_x0, band_y0, band_x1, band_y1,
2761 tbx0, tby0, tbx1, tby1, intersects);
2766 /** Returns whether a tile componenent is fully decoded, taking into account
2767 * p_tcd->win_* members.
2769 * @param p_tcd TCD handle.
2770 * @param compno Component number
2771 * @return OPJ_TRUE whether the tile componenent is fully decoded
2773 static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *p_tcd,
2776 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
2777 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
2778 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2779 /* with the tile coordinates */
2780 OPJ_UINT32 tcx0 = opj_uint_max(
2781 (OPJ_UINT32)tilec->x0,
2782 opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
2783 OPJ_UINT32 tcy0 = opj_uint_max(
2784 (OPJ_UINT32)tilec->y0,
2785 opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
2786 OPJ_UINT32 tcx1 = opj_uint_min(
2787 (OPJ_UINT32)tilec->x1,
2788 opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
2789 OPJ_UINT32 tcy1 = opj_uint_min(
2790 (OPJ_UINT32)tilec->y1,
2791 opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
2793 OPJ_UINT32 shift = tilec->numresolutions - tilec->minimum_num_resolutions;
2794 /* Tolerate small margin within the reduced resolution factor to consider if */
2795 /* the whole tile path must be taken */
2796 return (tcx0 >= (OPJ_UINT32)tilec->x0 &&
2797 tcy0 >= (OPJ_UINT32)tilec->y0 &&
2798 tcx1 <= (OPJ_UINT32)tilec->x1 &&
2799 tcy1 <= (OPJ_UINT32)tilec->y1 &&
2801 (((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 &&
2802 ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 &&
2803 (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 &&
2804 (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0)));