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_tcd_marker_info_t* p_marker_info,
186 opj_event_mgr_t *p_manager);
188 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
189 OPJ_BYTE * p_dest_data,
190 OPJ_UINT32 p_max_dest_size,
191 opj_codestream_info_t *p_cstr_info,
192 opj_event_mgr_t *p_manager);
195 static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *tcd,
198 /* ----------------------------------------------------------------------- */
201 Create a new TCD handle
203 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
205 opj_tcd_t *l_tcd = 00;
207 /* create the tcd structure */
208 l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
213 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
215 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
216 if (!l_tcd->tcd_image) {
225 /* ----------------------------------------------------------------------- */
227 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
231 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
232 opj_tcd_makelayer_fixed(tcd, layno, 1);
237 void opj_tcd_makelayer(opj_tcd_t *tcd,
242 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
245 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
247 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
249 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
250 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
252 for (resno = 0; resno < tilec->numresolutions; resno++) {
253 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
255 for (bandno = 0; bandno < res->numbands; bandno++) {
256 opj_tcd_band_t *band = &res->bands[bandno];
258 /* Skip empty bands */
259 if (opj_tcd_is_band_empty(band)) {
263 for (precno = 0; precno < res->pw * res->ph; precno++) {
264 opj_tcd_precinct_t *prc = &band->precincts[precno];
266 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
267 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
268 opj_tcd_layer_t *layer = &cblk->layers[layno];
272 cblk->numpassesinlayers = 0;
275 n = cblk->numpassesinlayers;
278 /* Special value to indicate to use all passes */
279 n = cblk->totalpasses;
281 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
284 opj_tcd_pass_t *pass = &cblk->passes[passno];
288 dd = pass->distortiondec;
290 dr = pass->rate - cblk->passes[n - 1].rate;
291 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
300 if (thresh - (dd / dr) <
301 DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
307 layer->numpasses = n - cblk->numpassesinlayers;
309 if (!layer->numpasses) {
314 if (cblk->numpassesinlayers == 0) {
315 layer->len = cblk->passes[n - 1].rate;
316 layer->data = cblk->data;
317 layer->disto = cblk->passes[n - 1].distortiondec;
319 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
321 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
322 layer->disto = cblk->passes[n - 1].distortiondec -
323 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
326 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
329 cblk->numpassesinlayers = n;
338 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
341 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
342 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
343 OPJ_INT32 matrice[10][10][3];
346 opj_cp_t *cp = tcd->cp;
347 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
348 opj_tcp_t *tcd_tcp = tcd->tcp;
350 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
351 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
353 for (i = 0; i < tcd_tcp->numlayers; i++) {
354 for (j = 0; j < tilec->numresolutions; j++) {
355 for (k = 0; k < 3; k++) {
357 (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
358 tilec->numresolutions * 3 + j * 3 + k]
359 * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
364 for (resno = 0; resno < tilec->numresolutions; resno++) {
365 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
367 for (bandno = 0; bandno < res->numbands; bandno++) {
368 opj_tcd_band_t *band = &res->bands[bandno];
370 /* Skip empty bands */
371 if (opj_tcd_is_band_empty(band)) {
375 for (precno = 0; precno < res->pw * res->ph; precno++) {
376 opj_tcd_precinct_t *prc = &band->precincts[precno];
378 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
379 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
380 opj_tcd_layer_t *layer = &cblk->layers[layno];
382 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
383 cblk->numbps); /* number of bit-plan equal to zero */
385 /* Correction of the matrix of coefficient to include the IMSB information */
387 value = matrice[layno][resno][bandno];
394 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
395 if (imsb >= matrice[layno - 1][resno][bandno]) {
396 value -= (imsb - matrice[layno - 1][resno][bandno]);
404 cblk->numpassesinlayers = 0;
407 n = cblk->numpassesinlayers;
408 if (cblk->numpassesinlayers == 0) {
410 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
412 n = cblk->numpassesinlayers;
415 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
418 layer->numpasses = n - cblk->numpassesinlayers;
420 if (!layer->numpasses) {
424 if (cblk->numpassesinlayers == 0) {
425 layer->len = cblk->passes[n - 1].rate;
426 layer->data = cblk->data;
428 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
430 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
434 cblk->numpassesinlayers = n;
443 OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
445 OPJ_UINT32 * p_data_written,
447 opj_codestream_info_t *cstr_info,
448 opj_event_mgr_t *p_manager)
450 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
452 OPJ_FLOAT64 min, max;
453 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
454 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
455 OPJ_FLOAT64 maxSE = 0;
457 opj_cp_t *cp = tcd->cp;
458 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
459 opj_tcp_t *tcd_tcp = tcd->tcp;
464 tcd_tile->numpix = 0; /* fixed_quality */
466 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
467 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
470 for (resno = 0; resno < tilec->numresolutions; resno++) {
471 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
473 for (bandno = 0; bandno < res->numbands; bandno++) {
474 opj_tcd_band_t *band = &res->bands[bandno];
476 /* Skip empty bands */
477 if (opj_tcd_is_band_empty(band)) {
481 for (precno = 0; precno < res->pw * res->ph; precno++) {
482 opj_tcd_precinct_t *prc = &band->precincts[precno];
484 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
485 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
487 for (passno = 0; passno < cblk->totalpasses; passno++) {
488 opj_tcd_pass_t *pass = &cblk->passes[passno];
490 OPJ_FLOAT64 dd, rdslope;
493 dr = (OPJ_INT32)pass->rate;
494 dd = pass->distortiondec;
496 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
497 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
515 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
516 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
522 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
523 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
524 * ((OPJ_FLOAT64)(tilec->numpix));
529 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
530 tile_info->numpix = tcd_tile->numpix;
531 tile_info->distotile = tcd_tile->distotile;
532 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
534 if (!tile_info->thresh) {
535 /* FIXME event manager error callback */
540 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
541 OPJ_FLOAT64 lo = min;
542 OPJ_FLOAT64 hi = max;
543 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
544 OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
545 OPJ_FLOAT64 goodthresh = 0;
546 OPJ_FLOAT64 stable_thresh = 0;
548 OPJ_FLOAT64 distotarget; /* fixed_quality */
551 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
552 tcd_tcp->distoratio[layno] / 10));
554 /* Don't try to find an optimal threshold but rather take everything not included yet, if
555 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
556 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
557 ==> possible to have some lossy layers and the last layer for sure lossless */
558 if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
559 (tcd_tcp->rates[layno] > 0.0f)) ||
560 ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
561 (tcd_tcp->distoratio[layno] > 0.0))) {
562 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
563 OPJ_FLOAT64 thresh = 0;
569 for (i = 0; i < 128; ++i) {
570 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
572 thresh = (lo + hi) / 2;
574 opj_tcd_makelayer(tcd, layno, thresh, 0);
576 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
577 if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) {
578 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
579 p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos,
581 THRESH_CALC, p_manager)) {
586 distoachieved = layno == 0 ?
587 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
589 if (distoachieved < distotarget) {
591 stable_thresh = thresh;
598 distoachieved = (layno == 0) ?
599 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
601 if (distoachieved < distotarget) {
603 stable_thresh = thresh;
609 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
610 p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos,
612 THRESH_CALC, p_manager)) {
613 /* TODO: what to do with l ??? seek / tell ??? */
614 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
620 stable_thresh = thresh;
624 goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
628 /* Special value to indicate to use all passes */
632 if (cstr_info) { /* Threshold for Marcela Index */
633 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
636 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
639 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
640 (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
646 OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
647 opj_image_t * p_image,
649 opj_thread_pool_t* p_tp)
651 p_tcd->image = p_image;
654 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
655 sizeof(opj_tcd_tile_t));
656 if (! p_tcd->tcd_image->tiles) {
660 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
661 p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
662 if (! p_tcd->tcd_image->tiles->comps) {
666 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
667 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
668 p_tcd->thread_pool = p_tp;
674 Destroy a previously created TCD handle
676 void opj_tcd_destroy(opj_tcd_t *tcd)
679 opj_tcd_free_tile(tcd);
681 if (tcd->tcd_image) {
682 opj_free(tcd->tcd_image);
686 opj_free(tcd->used_component);
692 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
694 if ((l_tilec->data == 00) ||
695 ((l_tilec->data_size_needed > l_tilec->data_size) &&
696 (l_tilec->ownsData == OPJ_FALSE))) {
697 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
698 if (!l_tilec->data && l_tilec->data_size_needed != 0) {
701 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
702 l_tilec->data_size = l_tilec->data_size_needed;
703 l_tilec->ownsData = OPJ_TRUE;
704 } else if (l_tilec->data_size_needed > l_tilec->data_size) {
705 /* We don't need to keep old data */
706 opj_image_data_free(l_tilec->data);
707 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
708 if (! l_tilec->data) {
709 l_tilec->data_size = 0;
710 l_tilec->data_size_needed = 0;
711 l_tilec->ownsData = OPJ_FALSE;
714 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
715 l_tilec->data_size = l_tilec->data_size_needed;
716 l_tilec->ownsData = OPJ_TRUE;
721 /* ----------------------------------------------------------------------- */
723 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
724 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
725 opj_event_mgr_t* manager)
727 OPJ_UINT32(*l_gain_ptr)(OPJ_UINT32) = 00;
728 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
729 opj_tcp_t * l_tcp = 00;
730 opj_cp_t * l_cp = 00;
731 opj_tcd_tile_t * l_tile = 00;
732 opj_tccp_t *l_tccp = 00;
733 opj_tcd_tilecomp_t *l_tilec = 00;
734 opj_image_comp_t * l_image_comp = 00;
735 opj_tcd_resolution_t *l_res = 00;
736 opj_tcd_band_t *l_band = 00;
737 opj_stepsize_t * l_step_size = 00;
738 opj_tcd_precinct_t *l_current_precinct = 00;
739 opj_image_t *l_image = 00;
741 OPJ_UINT32 l_level_no;
742 OPJ_UINT32 l_pdx, l_pdy;
744 OPJ_INT32 l_x0b, l_y0b;
745 OPJ_UINT32 l_tx0, l_ty0;
746 /* extent of precincts , top left, bottom right**/
747 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
748 /* number of precinct for a resolution */
749 OPJ_UINT32 l_nb_precincts;
750 /* room needed to store l_nb_precinct precinct for a resolution */
751 OPJ_UINT32 l_nb_precinct_size;
752 /* number of code blocks for a precinct*/
753 OPJ_UINT32 l_nb_code_blocks;
754 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
755 OPJ_UINT32 l_nb_code_blocks_size;
756 /* size of data for a tile */
757 OPJ_UINT32 l_data_size;
760 l_tcp = &(l_cp->tcps[p_tile_no]);
761 l_tile = p_tcd->tcd_image->tiles;
762 l_tccp = l_tcp->tccps;
763 l_tilec = l_tile->comps;
764 l_image = p_tcd->image;
765 l_image_comp = p_tcd->image->comps;
767 p = p_tile_no % l_cp->tw; /* tile coordinates */
768 q = p_tile_no / l_cp->tw;
769 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
771 /* 4 borders of the tile rescale on the image if necessary */
772 l_tx0 = l_cp->tx0 + p *
773 l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
774 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
775 l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
777 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
778 if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
779 opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
782 l_ty0 = l_cp->ty0 + q *
783 l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
784 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
785 l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
787 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
788 if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
789 opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
794 /* testcase 1888.pdf.asan.35.988 */
795 if (l_tccp->numresolutions == 0) {
796 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
799 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
801 /*tile->numcomps = image->numcomps; */
802 for (compno = 0; compno < l_tile->numcomps; ++compno) {
803 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
804 l_image_comp->resno_decoded = 0;
805 /* border of each l_tile component (global) */
806 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
807 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
808 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
809 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
810 l_tilec->compno = compno;
811 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
813 l_tilec->numresolutions = l_tccp->numresolutions;
814 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
815 l_tilec->minimum_num_resolutions = 1;
817 l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
818 l_cp->m_specific_param.m_dec.m_reduce;
822 OPJ_SIZE_T l_tile_data_size;
824 /* compute l_data_size with overflow check */
825 OPJ_SIZE_T w = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0);
826 OPJ_SIZE_T h = (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
828 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
829 if (h > 0 && w > SIZE_MAX / h) {
830 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
833 l_tile_data_size = w * h;
835 if (SIZE_MAX / sizeof(OPJ_UINT32) < l_tile_data_size) {
836 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
839 l_tile_data_size = l_tile_data_size * sizeof(OPJ_UINT32);
841 l_tilec->data_size_needed = l_tile_data_size;
844 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
845 opj_tcd_resolution_t);
847 opj_image_data_free(l_tilec->data_win);
848 l_tilec->data_win = NULL;
854 if (l_tilec->resolutions == 00) {
855 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
856 if (! l_tilec->resolutions) {
859 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
860 l_tilec->resolutions_size = l_data_size;
861 memset(l_tilec->resolutions, 0, l_data_size);
862 } else if (l_data_size > l_tilec->resolutions_size) {
863 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
864 l_tilec->resolutions, l_data_size);
865 if (! new_resolutions) {
866 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
867 opj_free(l_tilec->resolutions);
868 l_tilec->resolutions = NULL;
869 l_tilec->resolutions_size = 0;
872 l_tilec->resolutions = new_resolutions;
873 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
874 memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
875 l_data_size - l_tilec->resolutions_size);
876 l_tilec->resolutions_size = l_data_size;
879 l_level_no = l_tilec->numresolutions;
880 l_res = l_tilec->resolutions;
881 l_step_size = l_tccp->stepsizes;
882 if (l_tccp->qmfbid == 0) {
883 l_gain_ptr = &opj_dwt_getgain_real;
885 l_gain_ptr = &opj_dwt_getgain;
887 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
889 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
890 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
891 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
892 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
893 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
897 /* border for each resolution level (global) */
898 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
899 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
900 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
901 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
903 /*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);*/
904 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
905 l_pdx = l_tccp->prcw[resno];
906 l_pdy = l_tccp->prch[resno];
907 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
908 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
909 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
910 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
912 OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->x1,
913 (OPJ_INT32)l_pdx)) << l_pdx;
914 if (tmp > (OPJ_UINT32)INT_MAX) {
915 opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
918 l_br_prc_x_end = (OPJ_INT32)tmp;
921 OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->y1,
922 (OPJ_INT32)l_pdy)) << l_pdy;
923 if (tmp > (OPJ_UINT32)INT_MAX) {
924 opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
927 l_br_prc_y_end = (OPJ_INT32)tmp;
929 /*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 );*/
931 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
932 l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
933 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
934 l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
935 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
937 if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
938 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
941 l_nb_precincts = l_res->pw * l_res->ph;
943 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
945 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
948 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
951 tlcbgxstart = l_tl_prc_x_start;
952 tlcbgystart = l_tl_prc_y_start;
953 /*brcbgxend = l_br_prc_x_end;*/
954 /* brcbgyend = l_br_prc_y_end;*/
955 cbgwidthexpn = l_pdx;
956 cbgheightexpn = l_pdy;
959 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
960 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
961 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
962 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
963 cbgwidthexpn = l_pdx - 1;
964 cbgheightexpn = l_pdy - 1;
968 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
969 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
970 l_band = l_res->bands;
972 for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
974 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
978 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
979 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
980 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
981 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
983 l_band->bandno = bandno + 1;
984 /* x0b = 1 if bandno = 1 or 3 */
985 l_x0b = l_band->bandno & 1;
986 /* y0b = 1 if bandno = 2 or 3 */
987 l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
988 /* l_band border (global) */
989 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
990 l_level_no), (OPJ_INT32)(l_level_no + 1));
991 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
992 l_level_no), (OPJ_INT32)(l_level_no + 1));
993 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
994 l_level_no), (OPJ_INT32)(l_level_no + 1));
995 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
996 l_level_no), (OPJ_INT32)(l_level_no + 1));
1000 /* Skip empty bands */
1001 if (opj_tcd_is_band_empty(l_band)) {
1002 /* Do not zero l_band->precints to avoid leaks */
1003 /* but make sure we don't use it later, since */
1004 /* it will point to precincts of previous bands... */
1009 /** avoid an if with storing function pointer */
1010 l_gain = (*l_gain_ptr)(l_band->bandno);
1011 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
1012 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
1013 (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
1014 /* Mb value of Equation E-2 in "E.1 Inverse quantization
1015 * procedure" of the standard */
1016 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
1019 if (!l_band->precincts && (l_nb_precincts > 0U)) {
1020 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
1021 l_nb_precinct_size);
1022 if (! l_band->precincts) {
1023 opj_event_msg(manager, EVT_ERROR,
1024 "Not enough memory to handle band precints\n");
1027 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
1028 memset(l_band->precincts, 0, l_nb_precinct_size);
1029 l_band->precincts_data_size = l_nb_precinct_size;
1030 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
1032 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
1033 l_band->precincts,/*3 * */ l_nb_precinct_size);
1034 if (! new_precincts) {
1035 opj_event_msg(manager, EVT_ERROR,
1036 "Not enough memory to handle band precints\n");
1037 opj_free(l_band->precincts);
1038 l_band->precincts = NULL;
1039 l_band->precincts_data_size = 0;
1042 l_band->precincts = new_precincts;
1043 /*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);*/
1044 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
1045 l_nb_precinct_size - l_band->precincts_data_size);
1046 l_band->precincts_data_size = l_nb_precinct_size;
1049 l_current_precinct = l_band->precincts;
1050 for (precno = 0; precno < l_nb_precincts; ++precno) {
1051 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1052 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1053 (1 << cbgwidthexpn);
1054 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1055 (1 << cbgheightexpn);
1056 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1057 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1058 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1059 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1061 /* precinct size (global) */
1062 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1064 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1065 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1066 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1067 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1068 /*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);*/
1070 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1071 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1072 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1073 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1074 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1075 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1076 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1077 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1078 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1079 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1080 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1081 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1082 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1084 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1087 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1088 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
1089 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof_block) <
1091 opj_event_msg(manager, EVT_ERROR,
1092 "Size of code block data exceeds system limits\n");
1095 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1097 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1098 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1099 if (! l_current_precinct->cblks.blocks) {
1102 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1104 memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1106 l_current_precinct->block_size = l_nb_code_blocks_size;
1107 } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1108 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1109 l_nb_code_blocks_size);
1111 opj_free(l_current_precinct->cblks.blocks);
1112 l_current_precinct->cblks.blocks = NULL;
1113 l_current_precinct->block_size = 0;
1114 opj_event_msg(manager, EVT_ERROR,
1115 "Not enough memory for current precinct codeblock element\n");
1118 l_current_precinct->cblks.blocks = new_blocks;
1119 /*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); */
1121 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1122 l_current_precinct->block_size
1124 , l_nb_code_blocks_size - l_current_precinct->block_size);
1126 l_current_precinct->block_size = l_nb_code_blocks_size;
1129 if (! l_current_precinct->incltree) {
1130 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1131 l_current_precinct->ch, manager);
1133 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1134 l_current_precinct->cw, l_current_precinct->ch, manager);
1137 if (! l_current_precinct->imsbtree) {
1138 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1139 l_current_precinct->ch, manager);
1141 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1142 l_current_precinct->cw, l_current_precinct->ch, manager);
1145 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1146 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1147 l_current_precinct->cw) * (1 << cblkwidthexpn);
1148 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1149 l_current_precinct->cw) * (1 << cblkheightexpn);
1150 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1151 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1154 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1156 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1159 /* code-block size (global) */
1160 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1161 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1162 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1163 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1165 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1169 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1171 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1174 /* code-block size (global) */
1175 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1176 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1177 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1178 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1181 ++l_current_precinct;
1193 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1194 opj_event_mgr_t* p_manager)
1196 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1197 sizeof(opj_tcd_cblk_enc_t), p_manager);
1200 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1201 opj_event_mgr_t* p_manager)
1203 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1204 sizeof(opj_tcd_cblk_dec_t), p_manager);
1208 * Allocates memory for an encoding code block (but not data memory).
1210 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1213 if (! p_code_block->layers) {
1214 /* no memset since data */
1215 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1216 sizeof(opj_tcd_layer_t));
1217 if (! p_code_block->layers) {
1221 if (! p_code_block->passes) {
1222 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1223 sizeof(opj_tcd_pass_t));
1224 if (! p_code_block->passes) {
1232 * Allocates data memory for an encoding code block.
1234 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1237 OPJ_UINT32 l_data_size;
1239 /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1240 /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
1241 /* TODO: is there a theoretical upper-bound for the compressed code */
1243 l_data_size = 2 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1244 (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1246 if (l_data_size > p_code_block->data_size) {
1247 if (p_code_block->data) {
1248 /* We refer to data - 1 since below we incremented it */
1249 opj_free(p_code_block->data - 1);
1251 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1252 if (! p_code_block->data) {
1253 p_code_block->data_size = 0U;
1256 p_code_block->data_size = l_data_size;
1258 /* We reserve the initial byte as a fake byte to a non-FF value */
1259 /* and increment the data pointer, so that opj_mqc_init_enc() */
1260 /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1262 p_code_block->data[0] = 0;
1263 p_code_block->data += 1; /*why +1 ?*/
1269 void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1271 memset(seg, 0, sizeof(opj_tcd_seg_t));
1275 * Allocates memory for a decoding code block.
1277 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1280 if (! p_code_block->segs) {
1282 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1283 sizeof(opj_tcd_seg_t));
1284 if (! p_code_block->segs) {
1287 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1289 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1290 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1293 opj_tcd_seg_t * l_segs = p_code_block->segs;
1294 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1295 opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
1296 OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
1299 opj_aligned_free(p_code_block->decoded_data);
1300 p_code_block->decoded_data = 00;
1302 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1303 p_code_block->segs = l_segs;
1304 p_code_block->m_current_max_segs = l_current_max_segs;
1305 for (i = 0; i < l_current_max_segs; ++i) {
1306 opj_tcd_reinit_segment(&l_segs[i]);
1308 p_code_block->chunks = l_chunks;
1309 p_code_block->numchunksalloc = l_numchunksalloc;
1315 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd,
1316 OPJ_BOOL take_into_account_partial_decoding)
1319 OPJ_UINT32 l_data_size = 0;
1320 opj_image_comp_t * l_img_comp = 00;
1321 opj_tcd_tilecomp_t * l_tile_comp = 00;
1322 opj_tcd_resolution_t * l_res = 00;
1323 OPJ_UINT32 l_size_comp, l_remaining;
1326 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1327 l_img_comp = p_tcd->image->comps;
1329 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1331 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1332 l_remaining = l_img_comp->prec & 7; /* (%8) */
1338 if (l_size_comp == 3) {
1342 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1343 if (take_into_account_partial_decoding && !p_tcd->whole_tile_decoding) {
1344 w = l_res->win_x1 - l_res->win_x0;
1345 h = l_res->win_y1 - l_res->win_y0;
1347 w = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1348 h = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1350 if (h > 0 && UINT_MAX / w < h) {
1354 if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1357 l_temp *= l_size_comp;
1359 if (l_temp > UINT_MAX - l_data_size) {
1362 l_data_size += l_temp;
1370 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1371 OPJ_UINT32 p_tile_no,
1373 OPJ_UINT32 * p_data_written,
1374 OPJ_UINT32 p_max_length,
1375 opj_codestream_info_t *p_cstr_info,
1376 opj_tcd_marker_info_t* p_marker_info,
1377 opj_event_mgr_t *p_manager)
1380 if (p_tcd->cur_tp_num == 0) {
1382 p_tcd->tcd_tileno = p_tile_no;
1383 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1385 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1387 OPJ_UINT32 l_num_packs = 0;
1389 opj_tcd_tilecomp_t *l_tilec_idx =
1390 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1391 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1393 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1394 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1396 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1397 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1399 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1400 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1401 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1403 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1404 OPJ_SIZE_T)p_cstr_info->numcomps * (OPJ_SIZE_T)p_cstr_info->numlayers *
1406 sizeof(opj_packet_info_t));
1407 if (!p_cstr_info->tile[p_tile_no].packet) {
1408 /* FIXME event manager error callback */
1414 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1415 /*---------------TILE-------------------*/
1416 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1419 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1421 /* FIXME _ProfStart(PGROUP_MCT); */
1422 if (! opj_tcd_mct_encode(p_tcd)) {
1425 /* FIXME _ProfStop(PGROUP_MCT); */
1427 /* FIXME _ProfStart(PGROUP_DWT); */
1428 if (! opj_tcd_dwt_encode(p_tcd)) {
1431 /* FIXME _ProfStop(PGROUP_DWT); */
1433 /* FIXME _ProfStart(PGROUP_T1); */
1434 if (! opj_tcd_t1_encode(p_tcd)) {
1437 /* FIXME _ProfStop(PGROUP_T1); */
1439 /* FIXME _ProfStart(PGROUP_RATE); */
1440 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
1441 p_cstr_info, p_manager)) {
1444 /* FIXME _ProfStop(PGROUP_RATE); */
1447 /*--------------TIER2------------------*/
1451 p_cstr_info->index_write = 1;
1453 /* FIXME _ProfStart(PGROUP_T2); */
1455 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1456 p_cstr_info, p_marker_info, p_manager)) {
1459 /* FIXME _ProfStop(PGROUP_T2); */
1461 /*---------------CLEAN-------------------*/
1466 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1471 OPJ_UINT32 numcomps_to_decode,
1472 const OPJ_UINT32 *comps_indices,
1474 OPJ_UINT32 p_max_length,
1475 OPJ_UINT32 p_tile_no,
1476 opj_codestream_index_t *p_cstr_index,
1477 opj_event_mgr_t *p_manager
1480 OPJ_UINT32 l_data_read;
1483 p_tcd->tcd_tileno = p_tile_no;
1484 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1485 p_tcd->win_x0 = win_x0;
1486 p_tcd->win_y0 = win_y0;
1487 p_tcd->win_x1 = win_x1;
1488 p_tcd->win_y1 = win_y1;
1489 p_tcd->whole_tile_decoding = OPJ_TRUE;
1491 opj_free(p_tcd->used_component);
1492 p_tcd->used_component = NULL;
1494 if (numcomps_to_decode) {
1495 OPJ_BOOL* used_component = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
1496 p_tcd->image->numcomps);
1497 if (used_component == NULL) {
1500 for (compno = 0; compno < numcomps_to_decode; compno++) {
1501 used_component[ comps_indices[compno] ] = OPJ_TRUE;
1504 p_tcd->used_component = used_component;
1507 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1508 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1512 if (!opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno)) {
1513 p_tcd->whole_tile_decoding = OPJ_FALSE;
1518 if (p_tcd->whole_tile_decoding) {
1519 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1520 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1521 opj_tcd_resolution_t *l_res = &
1522 (tilec->resolutions[tilec->minimum_num_resolutions - 1]);
1523 OPJ_SIZE_T l_data_size;
1525 /* compute l_data_size with overflow check */
1526 OPJ_SIZE_T res_w = (OPJ_SIZE_T)(l_res->x1 - l_res->x0);
1527 OPJ_SIZE_T res_h = (OPJ_SIZE_T)(l_res->y1 - l_res->y0);
1529 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1533 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
1534 if (res_h > 0 && res_w > SIZE_MAX / res_h) {
1535 opj_event_msg(p_manager, EVT_ERROR,
1536 "Size of tile data exceeds system limits\n");
1539 l_data_size = res_w * res_h;
1541 if (SIZE_MAX / sizeof(OPJ_UINT32) < l_data_size) {
1542 opj_event_msg(p_manager, EVT_ERROR,
1543 "Size of tile data exceeds system limits\n");
1546 l_data_size *= sizeof(OPJ_UINT32);
1548 tilec->data_size_needed = l_data_size;
1550 if (!opj_alloc_tile_component_data(tilec)) {
1551 opj_event_msg(p_manager, EVT_ERROR,
1552 "Size of tile data exceeds system limits\n");
1557 /* Compute restricted tile-component and tile-resolution coordinates */
1558 /* of the window of interest, but defer the memory allocation until */
1559 /* we know the resno_decoded */
1560 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1562 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1563 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1565 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1569 /* Compute the intersection of the area of interest, expressed in tile coordinates */
1570 /* with the tile coordinates */
1571 tilec->win_x0 = opj_uint_max(
1572 (OPJ_UINT32)tilec->x0,
1573 opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
1574 tilec->win_y0 = opj_uint_max(
1575 (OPJ_UINT32)tilec->y0,
1576 opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
1577 tilec->win_x1 = opj_uint_min(
1578 (OPJ_UINT32)tilec->x1,
1579 opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
1580 tilec->win_y1 = opj_uint_min(
1581 (OPJ_UINT32)tilec->y1,
1582 opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
1583 if (tilec->win_x1 < tilec->win_x0 ||
1584 tilec->win_y1 < tilec->win_y0) {
1585 /* We should not normally go there. The circumstance is when */
1586 /* the tile coordinates do not intersect the area of interest */
1587 /* Upper level logic should not even try to decode that tile */
1588 opj_event_msg(p_manager, EVT_ERROR,
1589 "Invalid tilec->win_xxx values\n");
1593 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1594 opj_tcd_resolution_t *res = tilec->resolutions + resno;
1595 res->win_x0 = opj_uint_ceildivpow2(tilec->win_x0,
1596 tilec->numresolutions - 1 - resno);
1597 res->win_y0 = opj_uint_ceildivpow2(tilec->win_y0,
1598 tilec->numresolutions - 1 - resno);
1599 res->win_x1 = opj_uint_ceildivpow2(tilec->win_x1,
1600 tilec->numresolutions - 1 - resno);
1601 res->win_y1 = opj_uint_ceildivpow2(tilec->win_y1,
1602 tilec->numresolutions - 1 - resno);
1607 #ifdef TODO_MSD /* FIXME */
1610 OPJ_UINT32 resno, compno, numprec = 0;
1611 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1612 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1613 opj_tccp_t *tccp = &tcp->tccps[compno];
1614 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1615 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1616 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1617 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1618 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1619 numprec += res_idx->pw * res_idx->ph;
1620 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1621 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1624 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1625 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1626 p_cstr_info->packno = 0;
1631 /*--------------TIER2------------------*/
1632 /* FIXME _ProfStart(PGROUP_T2); */
1634 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1638 /* FIXME _ProfStop(PGROUP_T2); */
1640 /*------------------TIER1-----------------*/
1642 /* FIXME _ProfStart(PGROUP_T1); */
1643 if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1646 /* FIXME _ProfStop(PGROUP_T1); */
1649 /* For subtile decoding, now we know the resno_decoded, we can allocate */
1650 /* the tile data buffer */
1651 if (!p_tcd->whole_tile_decoding) {
1652 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1653 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1654 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1655 opj_tcd_resolution_t *res = tilec->resolutions + image_comp->resno_decoded;
1656 OPJ_SIZE_T w = res->win_x1 - res->win_x0;
1657 OPJ_SIZE_T h = res->win_y1 - res->win_y0;
1658 OPJ_SIZE_T l_data_size;
1660 opj_image_data_free(tilec->data_win);
1661 tilec->data_win = NULL;
1663 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1667 if (w > 0 && h > 0) {
1668 if (w > SIZE_MAX / h) {
1669 opj_event_msg(p_manager, EVT_ERROR,
1670 "Size of tile data exceeds system limits\n");
1673 l_data_size = w * h;
1674 if (l_data_size > SIZE_MAX / sizeof(OPJ_INT32)) {
1675 opj_event_msg(p_manager, EVT_ERROR,
1676 "Size of tile data exceeds system limits\n");
1679 l_data_size *= sizeof(OPJ_INT32);
1681 tilec->data_win = (OPJ_INT32*) opj_image_data_alloc(l_data_size);
1682 if (tilec->data_win == NULL) {
1683 opj_event_msg(p_manager, EVT_ERROR,
1684 "Size of tile data exceeds system limits\n");
1691 /*----------------DWT---------------------*/
1693 /* FIXME _ProfStart(PGROUP_DWT); */
1695 (! opj_tcd_dwt_decode(p_tcd)) {
1698 /* FIXME _ProfStop(PGROUP_DWT); */
1700 /*----------------MCT-------------------*/
1701 /* FIXME _ProfStart(PGROUP_MCT); */
1703 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1706 /* FIXME _ProfStop(PGROUP_MCT); */
1708 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1710 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1713 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1716 /*---------------TILE-------------------*/
1720 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1722 OPJ_UINT32 p_dest_length
1725 OPJ_UINT32 i, j, k, l_data_size = 0;
1726 opj_image_comp_t * l_img_comp = 00;
1727 opj_tcd_tilecomp_t * l_tilec = 00;
1728 opj_tcd_resolution_t * l_res;
1729 OPJ_UINT32 l_size_comp, l_remaining;
1730 OPJ_UINT32 l_stride, l_width, l_height;
1732 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, OPJ_TRUE);
1733 if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1737 l_tilec = p_tcd->tcd_image->tiles->comps;
1738 l_img_comp = p_tcd->image->comps;
1740 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1741 const OPJ_INT32* l_src_data;
1742 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1743 l_remaining = l_img_comp->prec & 7; /* (%8) */
1744 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1745 if (p_tcd->whole_tile_decoding) {
1746 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1747 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1748 l_stride = (OPJ_UINT32)(l_tilec->resolutions[l_tilec->minimum_num_resolutions -
1750 l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0) - l_width;
1751 l_src_data = l_tilec->data;
1753 l_width = l_res->win_x1 - l_res->win_x0;
1754 l_height = l_res->win_y1 - l_res->win_y0;
1756 l_src_data = l_tilec->data_win;
1763 if (l_size_comp == 3) {
1767 switch (l_size_comp) {
1769 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1770 const OPJ_INT32 * l_src_ptr = l_src_data;
1772 if (l_img_comp->sgnd) {
1773 for (j = 0; j < l_height; ++j) {
1774 for (k = 0; k < l_width; ++k) {
1775 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1777 l_src_ptr += l_stride;
1780 for (j = 0; j < l_height; ++j) {
1781 for (k = 0; k < l_width; ++k) {
1782 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1784 l_src_ptr += l_stride;
1788 p_dest = (OPJ_BYTE *)l_dest_ptr;
1792 const OPJ_INT32 * l_src_ptr = l_src_data;
1793 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1795 if (l_img_comp->sgnd) {
1796 for (j = 0; j < l_height; ++j) {
1797 for (k = 0; k < l_width; ++k) {
1798 OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1799 memcpy(l_dest_ptr, &val, sizeof(val));
1802 l_src_ptr += l_stride;
1805 for (j = 0; j < l_height; ++j) {
1806 for (k = 0; k < l_width; ++k) {
1807 OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1808 memcpy(l_dest_ptr, &val, sizeof(val));
1811 l_src_ptr += l_stride;
1815 p_dest = (OPJ_BYTE*) l_dest_ptr;
1819 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1820 const OPJ_INT32 * l_src_ptr = l_src_data;
1822 for (j = 0; j < l_height; ++j) {
1823 memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1824 l_dest_ptr += l_width;
1825 l_src_ptr += l_width + l_stride;
1828 p_dest = (OPJ_BYTE*) l_dest_ptr;
1843 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1845 OPJ_UINT32 compno, resno, bandno, precno;
1846 opj_tcd_tile_t *l_tile = 00;
1847 opj_tcd_tilecomp_t *l_tile_comp = 00;
1848 opj_tcd_resolution_t *l_res = 00;
1849 opj_tcd_band_t *l_band = 00;
1850 opj_tcd_precinct_t *l_precinct = 00;
1851 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1852 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1858 if (! p_tcd->tcd_image) {
1862 if (p_tcd->m_is_decoder) {
1863 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1865 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1868 l_tile = p_tcd->tcd_image->tiles;
1873 l_tile_comp = l_tile->comps;
1875 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1876 l_res = l_tile_comp->resolutions;
1879 l_nb_resolutions = l_tile_comp->resolutions_size / (OPJ_UINT32)sizeof(
1880 opj_tcd_resolution_t);
1881 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1882 l_band = l_res->bands;
1883 for (bandno = 0; bandno < 3; ++bandno) {
1884 l_precinct = l_band->precincts;
1887 l_nb_precincts = l_band->precincts_data_size / (OPJ_UINT32)sizeof(
1888 opj_tcd_precinct_t);
1889 for (precno = 0; precno < l_nb_precincts; ++precno) {
1890 opj_tgt_destroy(l_precinct->incltree);
1891 l_precinct->incltree = 00;
1892 opj_tgt_destroy(l_precinct->imsbtree);
1893 l_precinct->imsbtree = 00;
1894 (*l_tcd_code_block_deallocate)(l_precinct);
1898 opj_free(l_band->precincts);
1899 l_band->precincts = 00;
1906 opj_free(l_tile_comp->resolutions);
1907 l_tile_comp->resolutions = 00;
1910 if (l_tile_comp->ownsData && l_tile_comp->data) {
1911 opj_image_data_free(l_tile_comp->data);
1912 l_tile_comp->data = 00;
1913 l_tile_comp->ownsData = 0;
1914 l_tile_comp->data_size = 0;
1915 l_tile_comp->data_size_needed = 0;
1918 opj_image_data_free(l_tile_comp->data_win);
1923 opj_free(l_tile->comps);
1925 opj_free(p_tcd->tcd_image->tiles);
1926 p_tcd->tcd_image->tiles = 00;
1930 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1931 OPJ_BYTE * p_src_data,
1932 OPJ_UINT32 * p_data_read,
1933 OPJ_UINT32 p_max_src_size,
1934 opj_codestream_index_t *p_cstr_index,
1935 opj_event_mgr_t *p_manager
1940 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1945 if (! opj_t2_decode_packets(
1949 p_tcd->tcd_image->tiles,
1955 opj_t2_destroy(l_t2);
1959 opj_t2_destroy(l_t2);
1961 /*---------------CLEAN-------------------*/
1965 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1968 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1969 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1970 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1971 volatile OPJ_BOOL ret = OPJ_TRUE;
1972 OPJ_BOOL check_pterm = OPJ_FALSE;
1973 opj_mutex_t* p_manager_mutex = NULL;
1975 p_manager_mutex = opj_mutex_create();
1977 /* Only enable PTERM check if we decode all layers */
1978 if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
1979 (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
1980 check_pterm = OPJ_TRUE;
1983 for (compno = 0; compno < l_tile->numcomps;
1984 ++compno, ++l_tile_comp, ++l_tccp) {
1985 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1989 opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp,
1990 p_manager, p_manager_mutex, check_pterm);
1996 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1997 if (p_manager_mutex) {
1998 opj_mutex_destroy(p_manager_mutex);
2004 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
2007 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2008 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2009 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2010 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2012 for (compno = 0; compno < l_tile->numcomps;
2013 compno++, ++l_tile_comp, ++l_img_comp, ++l_tccp) {
2014 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2018 if (l_tccp->qmfbid == 1) {
2019 if (! opj_dwt_decode(p_tcd, l_tile_comp,
2020 l_img_comp->resno_decoded + 1)) {
2024 if (! opj_dwt_decode_real(p_tcd, l_tile_comp,
2025 l_img_comp->resno_decoded + 1)) {
2035 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
2037 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2038 opj_tcp_t * l_tcp = p_tcd->tcp;
2039 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2040 OPJ_SIZE_T l_samples;
2043 if (l_tcp->mct == 0 || p_tcd->used_component != NULL) {
2047 if (p_tcd->whole_tile_decoding) {
2048 opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2049 l_tile_comp->minimum_num_resolutions - 1;
2051 /* A bit inefficient: we process more data than needed if */
2052 /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */
2053 /* but we would need to take into account a stride then */
2054 l_samples = (OPJ_SIZE_T)(res_comp0->x1 - res_comp0->x0) *
2055 (OPJ_SIZE_T)(res_comp0->y1 - res_comp0->y0);
2056 if (l_tile->numcomps >= 3) {
2057 if (l_tile_comp->minimum_num_resolutions !=
2058 l_tile->comps[1].minimum_num_resolutions ||
2059 l_tile_comp->minimum_num_resolutions !=
2060 l_tile->comps[2].minimum_num_resolutions) {
2061 opj_event_msg(p_manager, EVT_ERROR,
2062 "Tiles don't all have the same dimension. Skip the MCT step.\n");
2066 if (l_tile->numcomps >= 3) {
2067 opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2068 l_tile_comp->minimum_num_resolutions - 1;
2069 opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2070 l_tile_comp->minimum_num_resolutions - 1;
2071 /* testcase 1336.pdf.asan.47.376 */
2072 if (p_tcd->image->comps[0].resno_decoded !=
2073 p_tcd->image->comps[1].resno_decoded ||
2074 p_tcd->image->comps[0].resno_decoded !=
2075 p_tcd->image->comps[2].resno_decoded ||
2076 (OPJ_SIZE_T)(res_comp1->x1 - res_comp1->x0) *
2077 (OPJ_SIZE_T)(res_comp1->y1 - res_comp1->y0) != l_samples ||
2078 (OPJ_SIZE_T)(res_comp2->x1 - res_comp2->x0) *
2079 (OPJ_SIZE_T)(res_comp2->y1 - res_comp2->y0) != l_samples) {
2080 opj_event_msg(p_manager, EVT_ERROR,
2081 "Tiles don't all have the same dimension. Skip the MCT step.\n");
2086 opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2087 p_tcd->image->comps[0].resno_decoded;
2089 l_samples = (OPJ_SIZE_T)(res_comp0->win_x1 - res_comp0->win_x0) *
2090 (OPJ_SIZE_T)(res_comp0->win_y1 - res_comp0->win_y0);
2091 if (l_tile->numcomps >= 3) {
2092 opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2093 p_tcd->image->comps[1].resno_decoded;
2094 opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2095 p_tcd->image->comps[2].resno_decoded;
2096 /* testcase 1336.pdf.asan.47.376 */
2097 if (p_tcd->image->comps[0].resno_decoded !=
2098 p_tcd->image->comps[1].resno_decoded ||
2099 p_tcd->image->comps[0].resno_decoded !=
2100 p_tcd->image->comps[2].resno_decoded ||
2101 (OPJ_SIZE_T)(res_comp1->win_x1 - res_comp1->win_x0) *
2102 (OPJ_SIZE_T)(res_comp1->win_y1 - res_comp1->win_y0) != l_samples ||
2103 (OPJ_SIZE_T)(res_comp2->win_x1 - res_comp2->win_x0) *
2104 (OPJ_SIZE_T)(res_comp2->win_y1 - res_comp2->win_y0) != l_samples) {
2105 opj_event_msg(p_manager, EVT_ERROR,
2106 "Tiles don't all have the same dimension. Skip the MCT step.\n");
2112 if (l_tile->numcomps >= 3) {
2113 if (l_tcp->mct == 2) {
2116 if (! l_tcp->m_mct_decoding_matrix) {
2120 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2125 for (i = 0; i < l_tile->numcomps; ++i) {
2126 if (p_tcd->whole_tile_decoding) {
2127 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2129 l_data[i] = (OPJ_BYTE*) l_tile_comp->data_win;
2134 if (! opj_mct_decode_custom(/* MCT data */
2135 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
2136 /* size of components */
2140 /* nb of components (i.e. size of pData) */
2142 /* tells if the data is signed */
2143 p_tcd->image->comps->sgnd)) {
2150 if (l_tcp->tccps->qmfbid == 1) {
2151 if (p_tcd->whole_tile_decoding) {
2152 opj_mct_decode(l_tile->comps[0].data,
2153 l_tile->comps[1].data,
2154 l_tile->comps[2].data,
2157 opj_mct_decode(l_tile->comps[0].data_win,
2158 l_tile->comps[1].data_win,
2159 l_tile->comps[2].data_win,
2163 if (p_tcd->whole_tile_decoding) {
2164 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
2165 (OPJ_FLOAT32*)l_tile->comps[1].data,
2166 (OPJ_FLOAT32*)l_tile->comps[2].data,
2169 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data_win,
2170 (OPJ_FLOAT32*)l_tile->comps[1].data_win,
2171 (OPJ_FLOAT32*)l_tile->comps[2].data_win,
2177 opj_event_msg(p_manager, EVT_ERROR,
2178 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
2186 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
2189 opj_tcd_tilecomp_t * l_tile_comp = 00;
2190 opj_tccp_t * l_tccp = 00;
2191 opj_image_comp_t * l_img_comp = 00;
2192 opj_tcd_resolution_t* l_res = 00;
2193 opj_tcd_tile_t * l_tile;
2194 OPJ_UINT32 l_width, l_height, i, j;
2195 OPJ_INT32 * l_current_ptr;
2196 OPJ_INT32 l_min, l_max;
2197 OPJ_UINT32 l_stride;
2199 l_tile = p_tcd->tcd_image->tiles;
2200 l_tile_comp = l_tile->comps;
2201 l_tccp = p_tcd->tcp->tccps;
2202 l_img_comp = p_tcd->image->comps;
2204 for (compno = 0; compno < l_tile->numcomps;
2205 compno++, ++l_img_comp, ++l_tccp, ++l_tile_comp) {
2207 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2211 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
2213 if (!p_tcd->whole_tile_decoding) {
2214 l_width = l_res->win_x1 - l_res->win_x0;
2215 l_height = l_res->win_y1 - l_res->win_y0;
2217 l_current_ptr = l_tile_comp->data_win;
2219 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
2220 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
2221 l_stride = (OPJ_UINT32)(
2222 l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x1 -
2223 l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x0)
2225 l_current_ptr = l_tile_comp->data;
2227 assert(l_height == 0 ||
2228 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
2231 if (l_img_comp->sgnd) {
2232 l_min = -(1 << (l_img_comp->prec - 1));
2233 l_max = (1 << (l_img_comp->prec - 1)) - 1;
2236 l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
2240 if (l_tccp->qmfbid == 1) {
2241 for (j = 0; j < l_height; ++j) {
2242 for (i = 0; i < l_width; ++i) {
2243 /* TODO: do addition on int64 ? */
2244 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
2248 l_current_ptr += l_stride;
2251 for (j = 0; j < l_height; ++j) {
2252 for (i = 0; i < l_width; ++i) {
2253 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
2254 if (l_value > INT_MAX) {
2255 *l_current_ptr = l_max;
2256 } else if (l_value < INT_MIN) {
2257 *l_current_ptr = l_min;
2259 /* Do addition on int64 to avoid overflows */
2260 OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value);
2261 *l_current_ptr = (OPJ_INT32)opj_int64_clamp(
2262 l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
2266 l_current_ptr += l_stride;
2277 * Deallocates the encoding data of the given precinct.
2279 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
2281 OPJ_UINT32 cblkno, l_nb_code_blocks;
2283 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
2285 /*fprintf(stderr,"deallocate codeblock:{\n");*/
2286 /*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);*/
2287 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
2288 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 );*/
2291 l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2292 opj_tcd_cblk_dec_t);
2293 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
2295 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2297 if (l_code_block->segs) {
2298 opj_free(l_code_block->segs);
2299 l_code_block->segs = 00;
2302 if (l_code_block->chunks) {
2303 opj_free(l_code_block->chunks);
2304 l_code_block->chunks = 00;
2307 opj_aligned_free(l_code_block->decoded_data);
2308 l_code_block->decoded_data = NULL;
2313 opj_free(p_precinct->cblks.dec);
2314 p_precinct->cblks.dec = 00;
2319 * Deallocates the encoding data of the given precinct.
2321 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
2323 OPJ_UINT32 cblkno, l_nb_code_blocks;
2325 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
2327 l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2328 opj_tcd_cblk_enc_t);
2330 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2331 if (l_code_block->data) {
2332 /* We refer to data - 1 since below we incremented it */
2333 /* in opj_tcd_code_block_enc_allocate_data() */
2334 opj_free(l_code_block->data - 1);
2335 l_code_block->data = 00;
2338 if (l_code_block->layers) {
2339 opj_free(l_code_block->layers);
2340 l_code_block->layers = 00;
2343 if (l_code_block->passes) {
2344 opj_free(l_code_block->passes);
2345 l_code_block->passes = 00;
2350 opj_free(p_precinct->cblks.enc);
2352 p_precinct->cblks.enc = 00;
2356 OPJ_SIZE_T opj_tcd_get_encoder_input_buffer_size(opj_tcd_t *p_tcd)
2359 OPJ_SIZE_T l_data_size = 0;
2360 opj_image_comp_t * l_img_comp = 00;
2361 opj_tcd_tilecomp_t * l_tilec = 00;
2362 OPJ_UINT32 l_size_comp, l_remaining;
2364 l_tilec = p_tcd->tcd_image->tiles->comps;
2365 l_img_comp = p_tcd->image->comps;
2366 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2367 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2368 l_remaining = l_img_comp->prec & 7; /* (%8) */
2374 if (l_size_comp == 3) {
2378 l_data_size += l_size_comp * ((OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2379 (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0));
2387 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2390 opj_tcd_tilecomp_t * l_tile_comp = 00;
2391 opj_tccp_t * l_tccp = 00;
2392 opj_image_comp_t * l_img_comp = 00;
2393 opj_tcd_tile_t * l_tile;
2394 OPJ_SIZE_T l_nb_elem, i;
2395 OPJ_INT32 * l_current_ptr;
2397 l_tile = p_tcd->tcd_image->tiles;
2398 l_tile_comp = l_tile->comps;
2399 l_tccp = p_tcd->tcp->tccps;
2400 l_img_comp = p_tcd->image->comps;
2402 for (compno = 0; compno < l_tile->numcomps; compno++) {
2403 l_current_ptr = l_tile_comp->data;
2404 l_nb_elem = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2405 (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2407 if (l_tccp->qmfbid == 1) {
2408 for (i = 0; i < l_nb_elem; ++i) {
2409 *l_current_ptr -= l_tccp->m_dc_level_shift ;
2413 for (i = 0; i < l_nb_elem; ++i) {
2414 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2427 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2429 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2430 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2431 OPJ_SIZE_T samples = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2432 (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2434 OPJ_BYTE ** l_data = 00;
2435 opj_tcp_t * l_tcp = p_tcd->tcp;
2437 if (!p_tcd->tcp->mct) {
2441 if (p_tcd->tcp->mct == 2) {
2442 if (! p_tcd->tcp->m_mct_coding_matrix) {
2446 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2451 for (i = 0; i < l_tile->numcomps; ++i) {
2452 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2456 if (! opj_mct_encode_custom(/* MCT data */
2457 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2458 /* size of components */
2462 /* nb of components (i.e. size of pData) */
2464 /* tells if the data is signed */
2465 p_tcd->image->comps->sgnd)) {
2471 } else if (l_tcp->tccps->qmfbid == 0) {
2472 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2473 l_tile->comps[2].data, samples);
2475 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2476 l_tile->comps[2].data, samples);
2482 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2484 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2485 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2486 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2489 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2490 if (l_tccp->qmfbid == 1) {
2491 if (! opj_dwt_encode(l_tile_comp)) {
2494 } else if (l_tccp->qmfbid == 0) {
2495 if (! opj_dwt_encode_real(l_tile_comp)) {
2507 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2510 const OPJ_FLOAT64 * l_mct_norms;
2511 OPJ_UINT32 l_mct_numcomps = 0U;
2512 opj_tcp_t * l_tcp = p_tcd->tcp;
2514 l_t1 = opj_t1_create(OPJ_TRUE);
2519 if (l_tcp->mct == 1) {
2520 l_mct_numcomps = 3U;
2521 /* irreversible encoding */
2522 if (l_tcp->tccps->qmfbid == 0) {
2523 l_mct_norms = opj_mct_get_mct_norms_real();
2525 l_mct_norms = opj_mct_get_mct_norms();
2528 l_mct_numcomps = p_tcd->image->numcomps;
2529 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2532 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2534 opj_t1_destroy(l_t1);
2538 opj_t1_destroy(l_t1);
2543 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2544 OPJ_BYTE * p_dest_data,
2545 OPJ_UINT32 * p_data_written,
2546 OPJ_UINT32 p_max_dest_size,
2547 opj_codestream_info_t *p_cstr_info,
2548 opj_tcd_marker_info_t* p_marker_info,
2549 opj_event_mgr_t *p_manager)
2553 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2558 if (! opj_t2_encode_packets(
2561 p_tcd->tcd_image->tiles,
2562 p_tcd->tcp->numlayers,
2573 opj_t2_destroy(l_t2);
2577 opj_t2_destroy(l_t2);
2579 /*---------------CLEAN-------------------*/
2584 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2585 OPJ_BYTE * p_dest_data,
2586 OPJ_UINT32 p_max_dest_size,
2587 opj_codestream_info_t *p_cstr_info,
2588 opj_event_mgr_t *p_manager)
2590 opj_cp_t * l_cp = p_tcd->cp;
2591 OPJ_UINT32 l_nb_written = 0;
2594 p_cstr_info->index_write = 0;
2597 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2598 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2600 /* Normal Rate/distortion allocation */
2601 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2602 p_cstr_info, p_manager)) {
2606 /* Fixed layer allocation */
2607 opj_tcd_rateallocate_fixed(p_tcd);
2614 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2616 OPJ_SIZE_T p_src_length)
2620 OPJ_SIZE_T l_data_size = 0;
2621 opj_image_comp_t * l_img_comp = 00;
2622 opj_tcd_tilecomp_t * l_tilec = 00;
2623 OPJ_UINT32 l_size_comp, l_remaining;
2624 OPJ_SIZE_T l_nb_elem;
2626 l_data_size = opj_tcd_get_encoder_input_buffer_size(p_tcd);
2627 if (l_data_size != p_src_length) {
2631 l_tilec = p_tcd->tcd_image->tiles->comps;
2632 l_img_comp = p_tcd->image->comps;
2633 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2634 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2635 l_remaining = l_img_comp->prec & 7; /* (%8) */
2636 l_nb_elem = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2637 (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
2643 if (l_size_comp == 3) {
2647 switch (l_size_comp) {
2649 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2650 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2652 if (l_img_comp->sgnd) {
2653 for (j = 0; j < l_nb_elem; ++j) {
2654 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2657 for (j = 0; j < l_nb_elem; ++j) {
2658 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2662 p_src = (OPJ_BYTE*) l_src_ptr;
2666 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2667 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2669 if (l_img_comp->sgnd) {
2670 for (j = 0; j < l_nb_elem; ++j) {
2671 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2674 for (j = 0; j < l_nb_elem; ++j) {
2675 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2679 p_src = (OPJ_BYTE*) l_src_ptr;
2683 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2684 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2686 for (j = 0; j < l_nb_elem; ++j) {
2687 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2690 p_src = (OPJ_BYTE*) l_src_ptr;
2702 OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2704 return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);
2707 OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd,
2716 /* Note: those values for filter_margin are in part the result of */
2717 /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
2718 /* to the maximum left/right extension given in tables F.2 and F.3 of the */
2719 /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
2720 /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
2721 /* needed to be bumped to 4, in case inconsistencies are found while */
2722 /* decoding parts of irreversible coded images. */
2723 /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
2724 OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3;
2725 opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]);
2726 opj_image_comp_t* image_comp = &(tcd->image->comps[compno]);
2727 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2728 /* with the tile coordinates */
2729 OPJ_UINT32 tcx0 = opj_uint_max(
2730 (OPJ_UINT32)tilec->x0,
2731 opj_uint_ceildiv(tcd->win_x0, image_comp->dx));
2732 OPJ_UINT32 tcy0 = opj_uint_max(
2733 (OPJ_UINT32)tilec->y0,
2734 opj_uint_ceildiv(tcd->win_y0, image_comp->dy));
2735 OPJ_UINT32 tcx1 = opj_uint_min(
2736 (OPJ_UINT32)tilec->x1,
2737 opj_uint_ceildiv(tcd->win_x1, image_comp->dx));
2738 OPJ_UINT32 tcy1 = opj_uint_min(
2739 (OPJ_UINT32)tilec->y1,
2740 opj_uint_ceildiv(tcd->win_y1, image_comp->dy));
2741 /* Compute number of decomposition for this band. See table F-1 */
2742 OPJ_UINT32 nb = (resno == 0) ?
2743 tilec->numresolutions - 1 :
2744 tilec->numresolutions - resno;
2745 /* Map above tile-based coordinates to sub-band-based coordinates per */
2746 /* equation B-15 of the standard */
2747 OPJ_UINT32 x0b = bandno & 1;
2748 OPJ_UINT32 y0b = bandno >> 1;
2749 OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 :
2750 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2751 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2752 OPJ_UINT32 tby0 = (nb == 0) ? tcy0 :
2753 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2754 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2755 OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 :
2756 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2757 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2758 OPJ_UINT32 tby1 = (nb == 0) ? tcy1 :
2759 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2760 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2761 OPJ_BOOL intersects;
2763 if (tbx0 < filter_margin) {
2766 tbx0 -= filter_margin;
2768 if (tby0 < filter_margin) {
2771 tby0 -= filter_margin;
2773 tbx1 = opj_uint_adds(tbx1, filter_margin);
2774 tby1 = opj_uint_adds(tby1, filter_margin);
2776 intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 &&
2779 #ifdef DEBUG_VERBOSE
2780 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",
2781 compno, resno, nb, bandno, x0b, y0b,
2782 band_x0, band_y0, band_x1, band_y1,
2783 tbx0, tby0, tbx1, tby1, intersects);
2788 /** Returns whether a tile componenent is fully decoded, taking into account
2789 * p_tcd->win_* members.
2791 * @param p_tcd TCD handle.
2792 * @param compno Component number
2793 * @return OPJ_TRUE whether the tile componenent is fully decoded
2795 static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *p_tcd,
2798 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
2799 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
2800 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2801 /* with the tile coordinates */
2802 OPJ_UINT32 tcx0 = opj_uint_max(
2803 (OPJ_UINT32)tilec->x0,
2804 opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
2805 OPJ_UINT32 tcy0 = opj_uint_max(
2806 (OPJ_UINT32)tilec->y0,
2807 opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
2808 OPJ_UINT32 tcx1 = opj_uint_min(
2809 (OPJ_UINT32)tilec->x1,
2810 opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
2811 OPJ_UINT32 tcy1 = opj_uint_min(
2812 (OPJ_UINT32)tilec->y1,
2813 opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
2815 OPJ_UINT32 shift = tilec->numresolutions - tilec->minimum_num_resolutions;
2816 /* Tolerate small margin within the reduced resolution factor to consider if */
2817 /* the whole tile path must be taken */
2818 return (tcx0 >= (OPJ_UINT32)tilec->x0 &&
2819 tcy0 >= (OPJ_UINT32)tilec->y0 &&
2820 tcx1 <= (OPJ_UINT32)tilec->x1 &&
2821 tcy1 <= (OPJ_UINT32)tilec->y1 &&
2823 (((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 &&
2824 ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 &&
2825 (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 &&
2826 (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0)));
2829 /* ----------------------------------------------------------------------- */
2831 opj_tcd_marker_info_t* opj_tcd_marker_info_create(OPJ_BOOL need_PLT)
2833 opj_tcd_marker_info_t *l_tcd_marker_info =
2834 (opj_tcd_marker_info_t*) opj_calloc(1, sizeof(opj_tcd_marker_info_t));
2835 if (!l_tcd_marker_info) {
2839 l_tcd_marker_info->need_PLT = need_PLT;
2841 return l_tcd_marker_info;
2844 /* ----------------------------------------------------------------------- */
2846 void opj_tcd_marker_info_destroy(opj_tcd_marker_info_t *p_tcd_marker_info)
2848 if (p_tcd_marker_info) {
2849 opj_free(p_tcd_marker_info->p_packet_size);
2853 /* ----------------------------------------------------------------------- */