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 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
42 #include "opj_common.h"
44 /* ----------------------------------------------------------------------- */
48 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
50 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
52 fprintf(fd, "image {\n");
53 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
54 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
57 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
58 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
59 fprintf(fd, " tile {\n");
60 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
61 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
62 for (compno = 0; compno < tile->numcomps; compno++) {
63 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
64 fprintf(fd, " tilec {\n");
66 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
67 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
68 for (resno = 0; resno < tilec->numresolutions; resno++) {
69 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
70 fprintf(fd, "\n res {\n");
72 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
73 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
74 for (bandno = 0; bandno < res->numbands; bandno++) {
75 opj_tcd_band_t *band = &res->bands[bandno];
76 fprintf(fd, " band {\n");
78 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
79 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
80 for (precno = 0; precno < res->pw * res->ph; precno++) {
81 opj_tcd_precinct_t *prec = &band->precincts[precno];
82 fprintf(fd, " prec {\n");
84 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
85 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
87 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
88 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
89 fprintf(fd, " cblk {\n");
91 " x0=%d, y0=%d, x1=%d, y1=%d\n",
92 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
111 * Initializes tile coding/decoding
113 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
114 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
115 opj_event_mgr_t* manager);
118 * Allocates memory for a decoding code block.
120 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
124 * Deallocates the decoding data of the given precinct.
126 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct);
129 * Allocates memory for an encoding code block (but not data).
131 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
135 * Allocates data for an encoding code block
137 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
141 * Deallocates the encoding data of the given precinct.
143 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct);
147 Free the memory allocated for encoding
148 @param tcd TCD handle
150 static void opj_tcd_free_tile(opj_tcd_t *tcd);
153 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
154 OPJ_BYTE * p_src_data,
155 OPJ_UINT32 * p_data_read,
156 OPJ_UINT32 p_max_src_size,
157 opj_codestream_index_t *p_cstr_index,
158 opj_event_mgr_t *p_manager);
160 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd,
161 opj_event_mgr_t *p_manager);
163 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd);
165 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd,
166 opj_event_mgr_t *p_manager);
168 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd);
171 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd);
173 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd);
175 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd);
177 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd);
179 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
180 OPJ_BYTE * p_dest_data,
181 OPJ_UINT32 * p_data_written,
182 OPJ_UINT32 p_max_dest_size,
183 opj_codestream_info_t *p_cstr_info,
184 opj_event_mgr_t *p_manager);
186 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
187 OPJ_BYTE * p_dest_data,
188 OPJ_UINT32 p_max_dest_size,
189 opj_codestream_info_t *p_cstr_info,
190 opj_event_mgr_t *p_manager);
192 /* ----------------------------------------------------------------------- */
195 Create a new TCD handle
197 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
199 opj_tcd_t *l_tcd = 00;
201 /* create the tcd structure */
202 l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
207 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
209 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
210 if (!l_tcd->tcd_image) {
219 /* ----------------------------------------------------------------------- */
221 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
225 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
226 opj_tcd_makelayer_fixed(tcd, layno, 1);
231 void opj_tcd_makelayer(opj_tcd_t *tcd,
236 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
239 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
241 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
243 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
244 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
246 for (resno = 0; resno < tilec->numresolutions; resno++) {
247 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
249 for (bandno = 0; bandno < res->numbands; bandno++) {
250 opj_tcd_band_t *band = &res->bands[bandno];
252 /* Skip empty bands */
253 if (opj_tcd_is_band_empty(band)) {
257 for (precno = 0; precno < res->pw * res->ph; precno++) {
258 opj_tcd_precinct_t *prc = &band->precincts[precno];
260 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
261 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
262 opj_tcd_layer_t *layer = &cblk->layers[layno];
266 cblk->numpassesinlayers = 0;
269 n = cblk->numpassesinlayers;
271 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
274 opj_tcd_pass_t *pass = &cblk->passes[passno];
278 dd = pass->distortiondec;
280 dr = pass->rate - cblk->passes[n - 1].rate;
281 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
290 if (thresh - (dd / dr) <
291 DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
296 layer->numpasses = n - cblk->numpassesinlayers;
298 if (!layer->numpasses) {
303 if (cblk->numpassesinlayers == 0) {
304 layer->len = cblk->passes[n - 1].rate;
305 layer->data = cblk->data;
306 layer->disto = cblk->passes[n - 1].distortiondec;
308 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
310 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
311 layer->disto = cblk->passes[n - 1].distortiondec -
312 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
315 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
318 cblk->numpassesinlayers = n;
327 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
330 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
331 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
332 OPJ_INT32 matrice[10][10][3];
335 opj_cp_t *cp = tcd->cp;
336 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
337 opj_tcp_t *tcd_tcp = tcd->tcp;
339 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
340 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
342 for (i = 0; i < tcd_tcp->numlayers; i++) {
343 for (j = 0; j < tilec->numresolutions; j++) {
344 for (k = 0; k < 3; k++) {
346 (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
347 tilec->numresolutions * 3 + j * 3 + k]
348 * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
353 for (resno = 0; resno < tilec->numresolutions; resno++) {
354 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
356 for (bandno = 0; bandno < res->numbands; bandno++) {
357 opj_tcd_band_t *band = &res->bands[bandno];
359 /* Skip empty bands */
360 if (opj_tcd_is_band_empty(band)) {
364 for (precno = 0; precno < res->pw * res->ph; precno++) {
365 opj_tcd_precinct_t *prc = &band->precincts[precno];
367 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
368 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
369 opj_tcd_layer_t *layer = &cblk->layers[layno];
371 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
372 cblk->numbps); /* number of bit-plan equal to zero */
374 /* Correction of the matrix of coefficient to include the IMSB information */
376 value = matrice[layno][resno][bandno];
383 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
384 if (imsb >= matrice[layno - 1][resno][bandno]) {
385 value -= (imsb - matrice[layno - 1][resno][bandno]);
393 cblk->numpassesinlayers = 0;
396 n = cblk->numpassesinlayers;
397 if (cblk->numpassesinlayers == 0) {
399 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
401 n = cblk->numpassesinlayers;
404 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
407 layer->numpasses = n - cblk->numpassesinlayers;
409 if (!layer->numpasses) {
413 if (cblk->numpassesinlayers == 0) {
414 layer->len = cblk->passes[n - 1].rate;
415 layer->data = cblk->data;
417 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
419 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
423 cblk->numpassesinlayers = n;
432 OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
434 OPJ_UINT32 * p_data_written,
436 opj_codestream_info_t *cstr_info,
437 opj_event_mgr_t *p_manager)
439 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
441 OPJ_FLOAT64 min, max;
442 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
443 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
444 OPJ_FLOAT64 maxSE = 0;
446 opj_cp_t *cp = tcd->cp;
447 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
448 opj_tcp_t *tcd_tcp = tcd->tcp;
453 tcd_tile->numpix = 0; /* fixed_quality */
455 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
456 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
459 for (resno = 0; resno < tilec->numresolutions; resno++) {
460 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
462 for (bandno = 0; bandno < res->numbands; bandno++) {
463 opj_tcd_band_t *band = &res->bands[bandno];
465 /* Skip empty bands */
466 if (opj_tcd_is_band_empty(band)) {
470 for (precno = 0; precno < res->pw * res->ph; precno++) {
471 opj_tcd_precinct_t *prc = &band->precincts[precno];
473 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
474 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
476 for (passno = 0; passno < cblk->totalpasses; passno++) {
477 opj_tcd_pass_t *pass = &cblk->passes[passno];
479 OPJ_FLOAT64 dd, rdslope;
482 dr = (OPJ_INT32)pass->rate;
483 dd = pass->distortiondec;
485 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
486 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
504 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
505 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
511 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
512 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
513 * ((OPJ_FLOAT64)(tilec->numpix));
518 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
519 tile_info->numpix = tcd_tile->numpix;
520 tile_info->distotile = tcd_tile->distotile;
521 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
523 if (!tile_info->thresh) {
524 /* FIXME event manager error callback */
529 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
530 OPJ_FLOAT64 lo = min;
531 OPJ_FLOAT64 hi = max;
532 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
533 OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
534 OPJ_FLOAT64 goodthresh = 0;
535 OPJ_FLOAT64 stable_thresh = 0;
537 OPJ_FLOAT64 distotarget; /* fixed_quality */
540 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
541 tcd_tcp->distoratio[layno] / 10));
543 /* Don't try to find an optimal threshold but rather take everything not included yet, if
544 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
545 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
546 ==> possible to have some lossy layers and the last layer for sure lossless */
547 if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
548 (tcd_tcp->rates[layno] > 0.0f)) ||
549 ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
550 (tcd_tcp->distoratio[layno] > 0.0))) {
551 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
552 OPJ_FLOAT64 thresh = 0;
558 for (i = 0; i < 128; ++i) {
559 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
561 thresh = (lo + hi) / 2;
563 opj_tcd_makelayer(tcd, layno, thresh, 0);
565 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
566 if (OPJ_IS_CINEMA(cp->rsiz)) {
567 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
568 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
569 THRESH_CALC, p_manager)) {
574 distoachieved = layno == 0 ?
575 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
577 if (distoachieved < distotarget) {
579 stable_thresh = thresh;
586 distoachieved = (layno == 0) ?
587 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
589 if (distoachieved < distotarget) {
591 stable_thresh = thresh;
597 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
598 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
599 THRESH_CALC, p_manager)) {
600 /* TODO: what to do with l ??? seek / tell ??? */
601 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
607 stable_thresh = thresh;
611 goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
618 if (cstr_info) { /* Threshold for Marcela Index */
619 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
622 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
625 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
626 (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
632 OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
633 opj_image_t * p_image,
635 opj_thread_pool_t* p_tp)
637 p_tcd->image = p_image;
640 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
641 sizeof(opj_tcd_tile_t));
642 if (! p_tcd->tcd_image->tiles) {
646 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
647 p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
648 if (! p_tcd->tcd_image->tiles->comps) {
652 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
653 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
654 p_tcd->thread_pool = p_tp;
660 Destroy a previously created TCD handle
662 void opj_tcd_destroy(opj_tcd_t *tcd)
665 opj_tcd_free_tile(tcd);
667 if (tcd->tcd_image) {
668 opj_free(tcd->tcd_image);
675 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
677 if ((l_tilec->data == 00) ||
678 ((l_tilec->data_size_needed > l_tilec->data_size) &&
679 (l_tilec->ownsData == OPJ_FALSE))) {
680 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
681 if (! l_tilec->data) {
684 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
685 l_tilec->data_size = l_tilec->data_size_needed;
686 l_tilec->ownsData = OPJ_TRUE;
687 } else if (l_tilec->data_size_needed > l_tilec->data_size) {
688 /* We don't need to keep old data */
689 opj_image_data_free(l_tilec->data);
690 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
691 if (! l_tilec->data) {
692 l_tilec->data_size = 0;
693 l_tilec->data_size_needed = 0;
694 l_tilec->ownsData = OPJ_FALSE;
697 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
698 l_tilec->data_size = l_tilec->data_size_needed;
699 l_tilec->ownsData = OPJ_TRUE;
704 /* ----------------------------------------------------------------------- */
706 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
707 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
708 opj_event_mgr_t* manager)
710 OPJ_UINT32(*l_gain_ptr)(OPJ_UINT32) = 00;
711 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
712 opj_tcp_t * l_tcp = 00;
713 opj_cp_t * l_cp = 00;
714 opj_tcd_tile_t * l_tile = 00;
715 opj_tccp_t *l_tccp = 00;
716 opj_tcd_tilecomp_t *l_tilec = 00;
717 opj_image_comp_t * l_image_comp = 00;
718 opj_tcd_resolution_t *l_res = 00;
719 opj_tcd_band_t *l_band = 00;
720 opj_stepsize_t * l_step_size = 00;
721 opj_tcd_precinct_t *l_current_precinct = 00;
722 opj_image_t *l_image = 00;
724 OPJ_UINT32 l_level_no;
725 OPJ_UINT32 l_pdx, l_pdy;
727 OPJ_INT32 l_x0b, l_y0b;
728 OPJ_UINT32 l_tx0, l_ty0;
729 /* extent of precincts , top left, bottom right**/
730 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
731 /* number of precinct for a resolution */
732 OPJ_UINT32 l_nb_precincts;
733 /* room needed to store l_nb_precinct precinct for a resolution */
734 OPJ_UINT32 l_nb_precinct_size;
735 /* number of code blocks for a precinct*/
736 OPJ_UINT32 l_nb_code_blocks;
737 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
738 OPJ_UINT32 l_nb_code_blocks_size;
739 /* size of data for a tile */
740 OPJ_UINT32 l_data_size;
743 l_tcp = &(l_cp->tcps[p_tile_no]);
744 l_tile = p_tcd->tcd_image->tiles;
745 l_tccp = l_tcp->tccps;
746 l_tilec = l_tile->comps;
747 l_image = p_tcd->image;
748 l_image_comp = p_tcd->image->comps;
750 p = p_tile_no % l_cp->tw; /* tile coordinates */
751 q = p_tile_no / l_cp->tw;
752 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
754 /* 4 borders of the tile rescale on the image if necessary */
755 l_tx0 = l_cp->tx0 + p *
756 l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
757 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
758 l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
760 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
761 if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
762 opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
765 l_ty0 = l_cp->ty0 + q *
766 l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
767 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
768 l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
770 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
771 if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
772 opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
777 /* testcase 1888.pdf.asan.35.988 */
778 if (l_tccp->numresolutions == 0) {
779 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
782 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
784 /*tile->numcomps = image->numcomps; */
785 for (compno = 0; compno < l_tile->numcomps; ++compno) {
786 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
787 l_image_comp->resno_decoded = 0;
788 /* border of each l_tile component (global) */
789 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
790 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
791 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
792 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
793 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
795 /* compute l_data_size with overflow check */
796 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
797 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
798 if ((l_data_size > 0U) &&
799 ((((OPJ_UINT32) - 1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 -
801 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
804 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
806 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
807 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
810 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
811 l_tilec->numresolutions = l_tccp->numresolutions;
812 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
813 l_tilec->minimum_num_resolutions = 1;
815 l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
816 l_cp->m_specific_param.m_dec.m_reduce;
819 l_tilec->data_size_needed = l_data_size;
820 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
821 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
825 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
826 opj_tcd_resolution_t);
828 if (l_tilec->resolutions == 00) {
829 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
830 if (! l_tilec->resolutions) {
833 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
834 l_tilec->resolutions_size = l_data_size;
835 memset(l_tilec->resolutions, 0, l_data_size);
836 } else if (l_data_size > l_tilec->resolutions_size) {
837 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
838 l_tilec->resolutions, l_data_size);
839 if (! new_resolutions) {
840 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
841 opj_free(l_tilec->resolutions);
842 l_tilec->resolutions = NULL;
843 l_tilec->resolutions_size = 0;
846 l_tilec->resolutions = new_resolutions;
847 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
848 memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
849 l_data_size - l_tilec->resolutions_size);
850 l_tilec->resolutions_size = l_data_size;
853 l_level_no = l_tilec->numresolutions;
854 l_res = l_tilec->resolutions;
855 l_step_size = l_tccp->stepsizes;
856 if (l_tccp->qmfbid == 0) {
857 l_gain_ptr = &opj_dwt_getgain_real;
859 l_gain_ptr = &opj_dwt_getgain;
861 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
863 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
864 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
865 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
866 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
867 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
871 /* border for each resolution level (global) */
872 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
873 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
874 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
875 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
876 /*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);*/
877 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
878 l_pdx = l_tccp->prcw[resno];
879 l_pdy = l_tccp->prch[resno];
880 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
881 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
882 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
883 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
884 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
885 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
886 /*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 );*/
888 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
889 l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
890 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
891 l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
892 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
894 if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
895 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
898 l_nb_precincts = l_res->pw * l_res->ph;
900 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
902 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
905 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
908 tlcbgxstart = l_tl_prc_x_start;
909 tlcbgystart = l_tl_prc_y_start;
910 /*brcbgxend = l_br_prc_x_end;*/
911 /* brcbgyend = l_br_prc_y_end;*/
912 cbgwidthexpn = l_pdx;
913 cbgheightexpn = l_pdy;
916 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
917 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
918 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
919 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
920 cbgwidthexpn = l_pdx - 1;
921 cbgheightexpn = l_pdy - 1;
925 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
926 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
927 l_band = l_res->bands;
929 for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
931 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
935 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
936 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
937 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
938 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
940 l_band->bandno = bandno + 1;
941 /* x0b = 1 if bandno = 1 or 3 */
942 l_x0b = l_band->bandno & 1;
943 /* y0b = 1 if bandno = 2 or 3 */
944 l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
945 /* l_band border (global) */
946 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
947 l_level_no), (OPJ_INT32)(l_level_no + 1));
948 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
949 l_level_no), (OPJ_INT32)(l_level_no + 1));
950 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
951 l_level_no), (OPJ_INT32)(l_level_no + 1));
952 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
953 l_level_no), (OPJ_INT32)(l_level_no + 1));
957 /* Skip empty bands */
958 if (opj_tcd_is_band_empty(l_band)) {
959 /* Do not zero l_band->precints to avoid leaks */
960 /* but make sure we don't use it later, since */
961 /* it will point to precincts of previous bands... */
966 /** avoid an if with storing function pointer */
967 l_gain = (*l_gain_ptr)(l_band->bandno);
968 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
969 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
970 (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
971 /* Mb value of Equation E-2 in "E.1 Inverse quantization
972 * procedure" of the standard */
973 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
976 if (!l_band->precincts && (l_nb_precincts > 0U)) {
977 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
979 if (! l_band->precincts) {
980 opj_event_msg(manager, EVT_ERROR,
981 "Not enough memory to handle band precints\n");
984 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
985 memset(l_band->precincts, 0, l_nb_precinct_size);
986 l_band->precincts_data_size = l_nb_precinct_size;
987 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
989 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
990 l_band->precincts,/*3 * */ l_nb_precinct_size);
991 if (! new_precincts) {
992 opj_event_msg(manager, EVT_ERROR,
993 "Not enough memory to handle band precints\n");
994 opj_free(l_band->precincts);
995 l_band->precincts = NULL;
996 l_band->precincts_data_size = 0;
999 l_band->precincts = new_precincts;
1000 /*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);*/
1001 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
1002 l_nb_precinct_size - l_band->precincts_data_size);
1003 l_band->precincts_data_size = l_nb_precinct_size;
1006 l_current_precinct = l_band->precincts;
1007 for (precno = 0; precno < l_nb_precincts; ++precno) {
1008 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1009 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1010 (1 << cbgwidthexpn);
1011 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1012 (1 << cbgheightexpn);
1013 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1014 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1015 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1016 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1018 /* precinct size (global) */
1019 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1021 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1022 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1023 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1024 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1025 /*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);*/
1027 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1028 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1029 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1030 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1031 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1032 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1033 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1034 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1035 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1036 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1037 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1038 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1039 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1041 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1044 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1045 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
1046 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1048 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1049 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1050 if (! l_current_precinct->cblks.blocks) {
1053 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1055 memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1057 l_current_precinct->block_size = l_nb_code_blocks_size;
1058 } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1059 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1060 l_nb_code_blocks_size);
1062 opj_free(l_current_precinct->cblks.blocks);
1063 l_current_precinct->cblks.blocks = NULL;
1064 l_current_precinct->block_size = 0;
1065 opj_event_msg(manager, EVT_ERROR,
1066 "Not enough memory for current precinct codeblock element\n");
1069 l_current_precinct->cblks.blocks = new_blocks;
1070 /*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); */
1072 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1073 l_current_precinct->block_size
1075 , l_nb_code_blocks_size - l_current_precinct->block_size);
1077 l_current_precinct->block_size = l_nb_code_blocks_size;
1080 if (! l_current_precinct->incltree) {
1081 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1082 l_current_precinct->ch, manager);
1084 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1085 l_current_precinct->cw, l_current_precinct->ch, manager);
1088 if (! l_current_precinct->imsbtree) {
1089 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1090 l_current_precinct->ch, manager);
1092 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1093 l_current_precinct->cw, l_current_precinct->ch, manager);
1096 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1097 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1098 l_current_precinct->cw) * (1 << cblkwidthexpn);
1099 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1100 l_current_precinct->cw) * (1 << cblkheightexpn);
1101 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1102 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1105 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1107 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1110 /* code-block size (global) */
1111 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1112 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1113 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1114 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1116 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1120 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1122 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1125 /* code-block size (global) */
1126 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1127 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1128 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1129 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1132 ++l_current_precinct;
1144 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1145 opj_event_mgr_t* p_manager)
1147 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1148 sizeof(opj_tcd_cblk_enc_t), p_manager);
1151 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1152 opj_event_mgr_t* p_manager)
1154 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1155 sizeof(opj_tcd_cblk_dec_t), p_manager);
1159 * Allocates memory for an encoding code block (but not data memory).
1161 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1164 if (! p_code_block->layers) {
1165 /* no memset since data */
1166 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1167 sizeof(opj_tcd_layer_t));
1168 if (! p_code_block->layers) {
1172 if (! p_code_block->passes) {
1173 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1174 sizeof(opj_tcd_pass_t));
1175 if (! p_code_block->passes) {
1183 * Allocates data memory for an encoding code block.
1185 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1188 OPJ_UINT32 l_data_size;
1190 /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1191 /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
1192 /* TODO: is there a theoretical upper-bound for the compressed code */
1194 l_data_size = 2 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1195 (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1197 if (l_data_size > p_code_block->data_size) {
1198 if (p_code_block->data) {
1199 /* We refer to data - 1 since below we incremented it */
1200 opj_free(p_code_block->data - 1);
1202 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1203 if (! p_code_block->data) {
1204 p_code_block->data_size = 0U;
1207 p_code_block->data_size = l_data_size;
1209 /* We reserve the initial byte as a fake byte to a non-FF value */
1210 /* and increment the data pointer, so that opj_mqc_init_enc() */
1211 /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1213 p_code_block->data[0] = 0;
1214 p_code_block->data += 1; /*why +1 ?*/
1220 void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1222 memset(seg, 0, sizeof(opj_tcd_seg_t));
1226 * Allocates memory for a decoding code block.
1228 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1231 if (! p_code_block->segs) {
1233 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1234 sizeof(opj_tcd_seg_t));
1235 if (! p_code_block->segs) {
1238 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1240 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1241 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1244 opj_tcd_seg_t * l_segs = p_code_block->segs;
1245 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1246 opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
1247 OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
1250 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1251 p_code_block->segs = l_segs;
1252 p_code_block->m_current_max_segs = l_current_max_segs;
1253 for (i = 0; i < l_current_max_segs; ++i) {
1254 opj_tcd_reinit_segment(&l_segs[i]);
1256 p_code_block->chunks = l_chunks;
1257 p_code_block->numchunksalloc = l_numchunksalloc;
1263 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd)
1266 OPJ_UINT32 l_data_size = 0;
1267 opj_image_comp_t * l_img_comp = 00;
1268 opj_tcd_tilecomp_t * l_tile_comp = 00;
1269 opj_tcd_resolution_t * l_res = 00;
1270 OPJ_UINT32 l_size_comp, l_remaining;
1273 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1274 l_img_comp = p_tcd->image->comps;
1276 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1277 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1278 l_remaining = l_img_comp->prec & 7; /* (%8) */
1284 if (l_size_comp == 3) {
1288 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1289 l_temp = (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 -
1290 l_res->y0)); /* x1*y1 can't overflow */
1291 if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1294 l_temp *= l_size_comp;
1296 if (l_temp > UINT_MAX - l_data_size) {
1299 l_data_size += l_temp;
1307 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1308 OPJ_UINT32 p_tile_no,
1310 OPJ_UINT32 * p_data_written,
1311 OPJ_UINT32 p_max_length,
1312 opj_codestream_info_t *p_cstr_info,
1313 opj_event_mgr_t *p_manager)
1316 if (p_tcd->cur_tp_num == 0) {
1318 p_tcd->tcd_tileno = p_tile_no;
1319 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1321 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1323 OPJ_UINT32 l_num_packs = 0;
1325 opj_tcd_tilecomp_t *l_tilec_idx =
1326 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1327 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1329 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1330 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1332 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1333 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1335 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1336 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1337 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1339 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1340 size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs,
1341 sizeof(opj_packet_info_t));
1342 if (!p_cstr_info->tile[p_tile_no].packet) {
1343 /* FIXME event manager error callback */
1349 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1350 /*---------------TILE-------------------*/
1351 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1354 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1356 /* FIXME _ProfStart(PGROUP_MCT); */
1357 if (! opj_tcd_mct_encode(p_tcd)) {
1360 /* FIXME _ProfStop(PGROUP_MCT); */
1362 /* FIXME _ProfStart(PGROUP_DWT); */
1363 if (! opj_tcd_dwt_encode(p_tcd)) {
1366 /* FIXME _ProfStop(PGROUP_DWT); */
1368 /* FIXME _ProfStart(PGROUP_T1); */
1369 if (! opj_tcd_t1_encode(p_tcd)) {
1372 /* FIXME _ProfStop(PGROUP_T1); */
1374 /* FIXME _ProfStart(PGROUP_RATE); */
1375 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
1376 p_cstr_info, p_manager)) {
1379 /* FIXME _ProfStop(PGROUP_RATE); */
1382 /*--------------TIER2------------------*/
1386 p_cstr_info->index_write = 1;
1388 /* FIXME _ProfStart(PGROUP_T2); */
1390 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1391 p_cstr_info, p_manager)) {
1394 /* FIXME _ProfStop(PGROUP_T2); */
1396 /*---------------CLEAN-------------------*/
1401 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1403 OPJ_UINT32 p_max_length,
1404 OPJ_UINT32 p_tile_no,
1405 opj_codestream_index_t *p_cstr_index,
1406 opj_event_mgr_t *p_manager
1409 OPJ_UINT32 l_data_read;
1410 p_tcd->tcd_tileno = p_tile_no;
1411 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1413 #ifdef TODO_MSD /* FIXME */
1416 OPJ_UINT32 resno, compno, numprec = 0;
1417 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1418 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1419 opj_tccp_t *tccp = &tcp->tccps[compno];
1420 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1421 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1422 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1423 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1424 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1425 numprec += res_idx->pw * res_idx->ph;
1426 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1427 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1430 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1431 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1432 p_cstr_info->packno = 0;
1437 /*--------------TIER2------------------*/
1438 /* FIXME _ProfStart(PGROUP_T2); */
1440 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1444 /* FIXME _ProfStop(PGROUP_T2); */
1446 /*------------------TIER1-----------------*/
1448 /* FIXME _ProfStart(PGROUP_T1); */
1449 if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1452 /* FIXME _ProfStop(PGROUP_T1); */
1454 /*----------------DWT---------------------*/
1456 /* FIXME _ProfStart(PGROUP_DWT); */
1458 (! opj_tcd_dwt_decode(p_tcd)) {
1461 /* FIXME _ProfStop(PGROUP_DWT); */
1463 /*----------------MCT-------------------*/
1464 /* FIXME _ProfStart(PGROUP_MCT); */
1466 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1469 /* FIXME _ProfStop(PGROUP_MCT); */
1471 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1473 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1476 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1479 /*---------------TILE-------------------*/
1483 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1485 OPJ_UINT32 p_dest_length
1488 OPJ_UINT32 i, j, k, l_data_size = 0;
1489 opj_image_comp_t * l_img_comp = 00;
1490 opj_tcd_tilecomp_t * l_tilec = 00;
1491 opj_tcd_resolution_t * l_res;
1492 OPJ_UINT32 l_size_comp, l_remaining;
1493 OPJ_UINT32 l_stride, l_width, l_height;
1495 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1496 if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1500 l_tilec = p_tcd->tcd_image->tiles->comps;
1501 l_img_comp = p_tcd->image->comps;
1503 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1504 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1505 l_remaining = l_img_comp->prec & 7; /* (%8) */
1506 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1507 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1508 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1509 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1515 if (l_size_comp == 3) {
1519 switch (l_size_comp) {
1521 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1522 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1524 if (l_img_comp->sgnd) {
1525 for (j = 0; j < l_height; ++j) {
1526 for (k = 0; k < l_width; ++k) {
1527 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1529 l_src_ptr += l_stride;
1532 for (j = 0; j < l_height; ++j) {
1533 for (k = 0; k < l_width; ++k) {
1534 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1536 l_src_ptr += l_stride;
1540 p_dest = (OPJ_BYTE *)l_dest_ptr;
1544 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1545 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1547 if (l_img_comp->sgnd) {
1548 for (j = 0; j < l_height; ++j) {
1549 for (k = 0; k < l_width; ++k) {
1550 OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1551 memcpy(l_dest_ptr, &val, sizeof(val));
1554 l_src_ptr += l_stride;
1557 for (j = 0; j < l_height; ++j) {
1558 for (k = 0; k < l_width; ++k) {
1559 OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1560 memcpy(l_dest_ptr, &val, sizeof(val));
1563 l_src_ptr += l_stride;
1567 p_dest = (OPJ_BYTE*) l_dest_ptr;
1571 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1572 OPJ_INT32 * l_src_ptr = l_tilec->data;
1574 for (j = 0; j < l_height; ++j) {
1575 memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1576 l_dest_ptr += l_width;
1577 l_src_ptr += l_width + l_stride;
1580 p_dest = (OPJ_BYTE*) l_dest_ptr;
1595 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1597 OPJ_UINT32 compno, resno, bandno, precno;
1598 opj_tcd_tile_t *l_tile = 00;
1599 opj_tcd_tilecomp_t *l_tile_comp = 00;
1600 opj_tcd_resolution_t *l_res = 00;
1601 opj_tcd_band_t *l_band = 00;
1602 opj_tcd_precinct_t *l_precinct = 00;
1603 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1604 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1610 if (! p_tcd->tcd_image) {
1614 if (p_tcd->m_is_decoder) {
1615 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1617 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1620 l_tile = p_tcd->tcd_image->tiles;
1625 l_tile_comp = l_tile->comps;
1627 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1628 l_res = l_tile_comp->resolutions;
1631 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1632 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1633 l_band = l_res->bands;
1634 for (bandno = 0; bandno < 3; ++bandno) {
1635 l_precinct = l_band->precincts;
1638 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1639 for (precno = 0; precno < l_nb_precincts; ++precno) {
1640 opj_tgt_destroy(l_precinct->incltree);
1641 l_precinct->incltree = 00;
1642 opj_tgt_destroy(l_precinct->imsbtree);
1643 l_precinct->imsbtree = 00;
1644 (*l_tcd_code_block_deallocate)(l_precinct);
1648 opj_free(l_band->precincts);
1649 l_band->precincts = 00;
1656 opj_free(l_tile_comp->resolutions);
1657 l_tile_comp->resolutions = 00;
1660 if (l_tile_comp->ownsData && l_tile_comp->data) {
1661 opj_image_data_free(l_tile_comp->data);
1662 l_tile_comp->data = 00;
1663 l_tile_comp->ownsData = 0;
1664 l_tile_comp->data_size = 0;
1665 l_tile_comp->data_size_needed = 0;
1670 opj_free(l_tile->comps);
1672 opj_free(p_tcd->tcd_image->tiles);
1673 p_tcd->tcd_image->tiles = 00;
1677 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1678 OPJ_BYTE * p_src_data,
1679 OPJ_UINT32 * p_data_read,
1680 OPJ_UINT32 p_max_src_size,
1681 opj_codestream_index_t *p_cstr_index,
1682 opj_event_mgr_t *p_manager
1687 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1692 if (! opj_t2_decode_packets(
1695 p_tcd->tcd_image->tiles,
1701 opj_t2_destroy(l_t2);
1705 opj_t2_destroy(l_t2);
1707 /*---------------CLEAN-------------------*/
1711 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1714 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1715 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1716 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1717 volatile OPJ_BOOL ret = OPJ_TRUE;
1718 OPJ_BOOL check_pterm = OPJ_FALSE;
1719 opj_mutex_t* p_manager_mutex = NULL;
1721 p_manager_mutex = opj_mutex_create();
1723 /* Only enable PTERM check if we decode all layers */
1724 if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
1725 (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
1726 check_pterm = OPJ_TRUE;
1729 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1730 opj_t1_decode_cblks(p_tcd->thread_pool, &ret, l_tile_comp, l_tccp,
1731 p_manager, p_manager_mutex, check_pterm);
1739 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1740 if (p_manager_mutex) {
1741 opj_mutex_destroy(p_manager_mutex);
1747 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1750 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1751 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1752 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1753 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1755 for (compno = 0; compno < l_tile->numcomps; compno++) {
1757 if (tcd->cp->reduce != 0) {
1758 tcd->image->comps[compno].resno_decoded =
1759 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1760 if (tcd->image->comps[compno].resno_decoded < 0)
1765 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1766 if(numres2decode > 0){
1769 if (l_tccp->qmfbid == 1) {
1770 if (! opj_dwt_decode(p_tcd->thread_pool, l_tile_comp,
1771 l_img_comp->resno_decoded + 1)) {
1775 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded + 1)) {
1787 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1789 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1790 opj_tcp_t * l_tcp = p_tcd->tcp;
1791 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1792 OPJ_UINT32 l_samples, i;
1798 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1799 (l_tile_comp->y1 - l_tile_comp->y0));
1801 if (l_tile->numcomps >= 3) {
1802 /* testcase 1336.pdf.asan.47.376 */
1803 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 -
1804 l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1805 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 -
1806 l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1807 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 -
1808 l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1809 opj_event_msg(p_manager, EVT_ERROR,
1810 "Tiles don't all have the same dimension. Skip the MCT step.\n");
1812 } else if (l_tcp->mct == 2) {
1815 if (! l_tcp->m_mct_decoding_matrix) {
1819 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
1824 for (i = 0; i < l_tile->numcomps; ++i) {
1825 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1829 if (! opj_mct_decode_custom(/* MCT data */
1830 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1831 /* size of components */
1835 /* nb of components (i.e. size of pData) */
1837 /* tells if the data is signed */
1838 p_tcd->image->comps->sgnd)) {
1845 if (l_tcp->tccps->qmfbid == 1) {
1846 opj_mct_decode(l_tile->comps[0].data,
1847 l_tile->comps[1].data,
1848 l_tile->comps[2].data,
1851 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1852 (OPJ_FLOAT32*)l_tile->comps[1].data,
1853 (OPJ_FLOAT32*)l_tile->comps[2].data,
1858 opj_event_msg(p_manager, EVT_ERROR,
1859 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
1867 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
1870 opj_tcd_tilecomp_t * l_tile_comp = 00;
1871 opj_tccp_t * l_tccp = 00;
1872 opj_image_comp_t * l_img_comp = 00;
1873 opj_tcd_resolution_t* l_res = 00;
1874 opj_tcd_tile_t * l_tile;
1875 OPJ_UINT32 l_width, l_height, i, j;
1876 OPJ_INT32 * l_current_ptr;
1877 OPJ_INT32 l_min, l_max;
1878 OPJ_UINT32 l_stride;
1880 l_tile = p_tcd->tcd_image->tiles;
1881 l_tile_comp = l_tile->comps;
1882 l_tccp = p_tcd->tcp->tccps;
1883 l_img_comp = p_tcd->image->comps;
1885 for (compno = 0; compno < l_tile->numcomps; compno++) {
1886 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1887 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1888 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1889 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1891 assert(l_height == 0 ||
1892 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1894 if (l_img_comp->sgnd) {
1895 l_min = -(1 << (l_img_comp->prec - 1));
1896 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1899 l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
1902 l_current_ptr = l_tile_comp->data;
1904 if (l_tccp->qmfbid == 1) {
1905 for (j = 0; j < l_height; ++j) {
1906 for (i = 0; i < l_width; ++i) {
1907 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
1911 l_current_ptr += l_stride;
1914 for (j = 0; j < l_height; ++j) {
1915 for (i = 0; i < l_width; ++i) {
1916 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1917 OPJ_INT32 l_value_int = (OPJ_INT32)opj_lrintf(l_value);
1918 if (l_value > INT_MAX ||
1919 (l_value_int > 0 && l_tccp->m_dc_level_shift > 0 &&
1920 l_value_int > INT_MAX - l_tccp->m_dc_level_shift)) {
1921 *l_current_ptr = l_max;
1923 *l_current_ptr = opj_int_clamp(
1924 l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
1928 l_current_ptr += l_stride;
1943 * Deallocates the encoding data of the given precinct.
1945 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
1947 OPJ_UINT32 cblkno, l_nb_code_blocks;
1949 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1951 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1952 /*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);*/
1953 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1954 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 );*/
1957 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1958 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1960 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1962 if (l_code_block->segs) {
1963 opj_free(l_code_block->segs);
1964 l_code_block->segs = 00;
1967 if (l_code_block->chunks) {
1968 opj_free(l_code_block->chunks);
1969 l_code_block->chunks = 00;
1975 opj_free(p_precinct->cblks.dec);
1976 p_precinct->cblks.dec = 00;
1981 * Deallocates the encoding data of the given precinct.
1983 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
1985 OPJ_UINT32 cblkno, l_nb_code_blocks;
1987 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1989 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1991 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1992 if (l_code_block->data) {
1993 /* We refer to data - 1 since below we incremented it */
1994 /* in opj_tcd_code_block_enc_allocate_data() */
1995 opj_free(l_code_block->data - 1);
1996 l_code_block->data = 00;
1999 if (l_code_block->layers) {
2000 opj_free(l_code_block->layers);
2001 l_code_block->layers = 00;
2004 if (l_code_block->passes) {
2005 opj_free(l_code_block->passes);
2006 l_code_block->passes = 00;
2011 opj_free(p_precinct->cblks.enc);
2013 p_precinct->cblks.enc = 00;
2017 OPJ_UINT32 opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
2019 OPJ_UINT32 i, l_data_size = 0;
2020 opj_image_comp_t * l_img_comp = 00;
2021 opj_tcd_tilecomp_t * l_tilec = 00;
2022 OPJ_UINT32 l_size_comp, l_remaining;
2024 l_tilec = p_tcd->tcd_image->tiles->comps;
2025 l_img_comp = p_tcd->image->comps;
2026 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2027 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2028 l_remaining = l_img_comp->prec & 7; /* (%8) */
2034 if (l_size_comp == 3) {
2038 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) *
2039 (l_tilec->y1 - l_tilec->y0));
2047 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2050 opj_tcd_tilecomp_t * l_tile_comp = 00;
2051 opj_tccp_t * l_tccp = 00;
2052 opj_image_comp_t * l_img_comp = 00;
2053 opj_tcd_tile_t * l_tile;
2054 OPJ_UINT32 l_nb_elem, i;
2055 OPJ_INT32 * l_current_ptr;
2057 l_tile = p_tcd->tcd_image->tiles;
2058 l_tile_comp = l_tile->comps;
2059 l_tccp = p_tcd->tcp->tccps;
2060 l_img_comp = p_tcd->image->comps;
2062 for (compno = 0; compno < l_tile->numcomps; compno++) {
2063 l_current_ptr = l_tile_comp->data;
2064 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2065 (l_tile_comp->y1 - l_tile_comp->y0));
2067 if (l_tccp->qmfbid == 1) {
2068 for (i = 0; i < l_nb_elem; ++i) {
2069 *l_current_ptr -= l_tccp->m_dc_level_shift ;
2073 for (i = 0; i < l_nb_elem; ++i) {
2074 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2087 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2089 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2090 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2091 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2092 (l_tile_comp->y1 - l_tile_comp->y0));
2094 OPJ_BYTE ** l_data = 00;
2095 opj_tcp_t * l_tcp = p_tcd->tcp;
2097 if (!p_tcd->tcp->mct) {
2101 if (p_tcd->tcp->mct == 2) {
2102 if (! p_tcd->tcp->m_mct_coding_matrix) {
2106 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2111 for (i = 0; i < l_tile->numcomps; ++i) {
2112 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2116 if (! opj_mct_encode_custom(/* MCT data */
2117 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2118 /* size of components */
2122 /* nb of components (i.e. size of pData) */
2124 /* tells if the data is signed */
2125 p_tcd->image->comps->sgnd)) {
2131 } else if (l_tcp->tccps->qmfbid == 0) {
2132 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2133 l_tile->comps[2].data, samples);
2135 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2136 l_tile->comps[2].data, samples);
2142 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2144 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2145 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2146 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2149 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2150 if (l_tccp->qmfbid == 1) {
2151 if (! opj_dwt_encode(l_tile_comp)) {
2154 } else if (l_tccp->qmfbid == 0) {
2155 if (! opj_dwt_encode_real(l_tile_comp)) {
2167 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2170 const OPJ_FLOAT64 * l_mct_norms;
2171 OPJ_UINT32 l_mct_numcomps = 0U;
2172 opj_tcp_t * l_tcp = p_tcd->tcp;
2174 l_t1 = opj_t1_create(OPJ_TRUE);
2179 if (l_tcp->mct == 1) {
2180 l_mct_numcomps = 3U;
2181 /* irreversible encoding */
2182 if (l_tcp->tccps->qmfbid == 0) {
2183 l_mct_norms = opj_mct_get_mct_norms_real();
2185 l_mct_norms = opj_mct_get_mct_norms();
2188 l_mct_numcomps = p_tcd->image->numcomps;
2189 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2192 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2194 opj_t1_destroy(l_t1);
2198 opj_t1_destroy(l_t1);
2203 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2204 OPJ_BYTE * p_dest_data,
2205 OPJ_UINT32 * p_data_written,
2206 OPJ_UINT32 p_max_dest_size,
2207 opj_codestream_info_t *p_cstr_info,
2208 opj_event_mgr_t *p_manager)
2212 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2217 if (! opj_t2_encode_packets(
2220 p_tcd->tcd_image->tiles,
2221 p_tcd->tcp->numlayers,
2231 opj_t2_destroy(l_t2);
2235 opj_t2_destroy(l_t2);
2237 /*---------------CLEAN-------------------*/
2242 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2243 OPJ_BYTE * p_dest_data,
2244 OPJ_UINT32 p_max_dest_size,
2245 opj_codestream_info_t *p_cstr_info,
2246 opj_event_mgr_t *p_manager)
2248 opj_cp_t * l_cp = p_tcd->cp;
2249 OPJ_UINT32 l_nb_written = 0;
2252 p_cstr_info->index_write = 0;
2255 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2256 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2258 /* Normal Rate/distortion allocation */
2259 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2260 p_cstr_info, p_manager)) {
2264 /* Fixed layer allocation */
2265 opj_tcd_rateallocate_fixed(p_tcd);
2272 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2274 OPJ_UINT32 p_src_length)
2276 OPJ_UINT32 i, j, l_data_size = 0;
2277 opj_image_comp_t * l_img_comp = 00;
2278 opj_tcd_tilecomp_t * l_tilec = 00;
2279 OPJ_UINT32 l_size_comp, l_remaining;
2280 OPJ_UINT32 l_nb_elem;
2282 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2283 if (l_data_size != p_src_length) {
2287 l_tilec = p_tcd->tcd_image->tiles->comps;
2288 l_img_comp = p_tcd->image->comps;
2289 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2290 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2291 l_remaining = l_img_comp->prec & 7; /* (%8) */
2292 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 -
2299 if (l_size_comp == 3) {
2303 switch (l_size_comp) {
2305 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2306 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2308 if (l_img_comp->sgnd) {
2309 for (j = 0; j < l_nb_elem; ++j) {
2310 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2313 for (j = 0; j < l_nb_elem; ++j) {
2314 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2318 p_src = (OPJ_BYTE*) l_src_ptr;
2322 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2323 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2325 if (l_img_comp->sgnd) {
2326 for (j = 0; j < l_nb_elem; ++j) {
2327 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2330 for (j = 0; j < l_nb_elem; ++j) {
2331 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2335 p_src = (OPJ_BYTE*) l_src_ptr;
2339 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2340 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2342 for (j = 0; j < l_nb_elem; ++j) {
2343 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2346 p_src = (OPJ_BYTE*) l_src_ptr;
2358 OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2360 return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);