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);
185 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
186 OPJ_BYTE * p_dest_data,
187 OPJ_UINT32 p_max_dest_size,
188 opj_codestream_info_t *p_cstr_info);
190 /* ----------------------------------------------------------------------- */
193 Create a new TCD handle
195 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
197 opj_tcd_t *l_tcd = 00;
199 /* create the tcd structure */
200 l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
205 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
207 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
208 if (!l_tcd->tcd_image) {
217 /* ----------------------------------------------------------------------- */
219 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
223 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
224 opj_tcd_makelayer_fixed(tcd, layno, 1);
229 void opj_tcd_makelayer(opj_tcd_t *tcd,
234 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
237 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
239 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
241 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
242 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
244 for (resno = 0; resno < tilec->numresolutions; resno++) {
245 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
247 for (bandno = 0; bandno < res->numbands; bandno++) {
248 opj_tcd_band_t *band = &res->bands[bandno];
250 /* Skip empty bands */
251 if (opj_tcd_is_band_empty(band)) {
255 for (precno = 0; precno < res->pw * res->ph; precno++) {
256 opj_tcd_precinct_t *prc = &band->precincts[precno];
258 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
259 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
260 opj_tcd_layer_t *layer = &cblk->layers[layno];
264 cblk->numpassesinlayers = 0;
267 n = cblk->numpassesinlayers;
269 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
272 opj_tcd_pass_t *pass = &cblk->passes[passno];
276 dd = pass->distortiondec;
278 dr = pass->rate - cblk->passes[n - 1].rate;
279 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
288 if (thresh - (dd / dr) <
289 DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
294 layer->numpasses = n - cblk->numpassesinlayers;
296 if (!layer->numpasses) {
301 if (cblk->numpassesinlayers == 0) {
302 layer->len = cblk->passes[n - 1].rate;
303 layer->data = cblk->data;
304 layer->disto = cblk->passes[n - 1].distortiondec;
306 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
308 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
309 layer->disto = cblk->passes[n - 1].distortiondec -
310 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
313 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
316 cblk->numpassesinlayers = n;
325 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
328 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
329 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
330 OPJ_INT32 matrice[10][10][3];
333 opj_cp_t *cp = tcd->cp;
334 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
335 opj_tcp_t *tcd_tcp = tcd->tcp;
337 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
338 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
340 for (i = 0; i < tcd_tcp->numlayers; i++) {
341 for (j = 0; j < tilec->numresolutions; j++) {
342 for (k = 0; k < 3; k++) {
344 (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
345 tilec->numresolutions * 3 + j * 3 + k]
346 * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
351 for (resno = 0; resno < tilec->numresolutions; resno++) {
352 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
354 for (bandno = 0; bandno < res->numbands; bandno++) {
355 opj_tcd_band_t *band = &res->bands[bandno];
357 /* Skip empty bands */
358 if (opj_tcd_is_band_empty(band)) {
362 for (precno = 0; precno < res->pw * res->ph; precno++) {
363 opj_tcd_precinct_t *prc = &band->precincts[precno];
365 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
366 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
367 opj_tcd_layer_t *layer = &cblk->layers[layno];
369 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
370 cblk->numbps); /* number of bit-plan equal to zero */
372 /* Correction of the matrix of coefficient to include the IMSB information */
374 value = matrice[layno][resno][bandno];
381 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
382 if (imsb >= matrice[layno - 1][resno][bandno]) {
383 value -= (imsb - matrice[layno - 1][resno][bandno]);
391 cblk->numpassesinlayers = 0;
394 n = cblk->numpassesinlayers;
395 if (cblk->numpassesinlayers == 0) {
397 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
399 n = cblk->numpassesinlayers;
402 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
405 layer->numpasses = n - cblk->numpassesinlayers;
407 if (!layer->numpasses) {
411 if (cblk->numpassesinlayers == 0) {
412 layer->len = cblk->passes[n - 1].rate;
413 layer->data = cblk->data;
415 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
417 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
421 cblk->numpassesinlayers = n;
430 OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
432 OPJ_UINT32 * p_data_written,
434 opj_codestream_info_t *cstr_info)
436 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
438 OPJ_FLOAT64 min, max;
439 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
440 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
441 OPJ_FLOAT64 maxSE = 0;
443 opj_cp_t *cp = tcd->cp;
444 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
445 opj_tcp_t *tcd_tcp = tcd->tcp;
450 tcd_tile->numpix = 0; /* fixed_quality */
452 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
453 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
456 for (resno = 0; resno < tilec->numresolutions; resno++) {
457 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
459 for (bandno = 0; bandno < res->numbands; bandno++) {
460 opj_tcd_band_t *band = &res->bands[bandno];
462 /* Skip empty bands */
463 if (opj_tcd_is_band_empty(band)) {
467 for (precno = 0; precno < res->pw * res->ph; precno++) {
468 opj_tcd_precinct_t *prc = &band->precincts[precno];
470 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
471 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
473 for (passno = 0; passno < cblk->totalpasses; passno++) {
474 opj_tcd_pass_t *pass = &cblk->passes[passno];
476 OPJ_FLOAT64 dd, rdslope;
479 dr = (OPJ_INT32)pass->rate;
480 dd = pass->distortiondec;
482 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
483 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
501 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
502 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
508 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
509 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
510 * ((OPJ_FLOAT64)(tilec->numpix));
515 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
516 tile_info->numpix = tcd_tile->numpix;
517 tile_info->distotile = tcd_tile->distotile;
518 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
520 if (!tile_info->thresh) {
521 /* FIXME event manager error callback */
526 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
527 OPJ_FLOAT64 lo = min;
528 OPJ_FLOAT64 hi = max;
529 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
530 OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
531 OPJ_FLOAT64 goodthresh = 0;
532 OPJ_FLOAT64 stable_thresh = 0;
534 OPJ_FLOAT64 distotarget; /* fixed_quality */
537 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
538 tcd_tcp->distoratio[layno] / 10));
540 /* Don't try to find an optimal threshold but rather take everything not included yet, if
541 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
542 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
543 ==> possible to have some lossy layers and the last layer for sure lossless */
544 if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
545 (tcd_tcp->rates[layno] > 0.0f)) ||
546 ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
547 (tcd_tcp->distoratio[layno] > 0.0))) {
548 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
549 OPJ_FLOAT64 thresh = 0;
555 for (i = 0; i < 128; ++i) {
556 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
558 thresh = (lo + hi) / 2;
560 opj_tcd_makelayer(tcd, layno, thresh, 0);
562 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
563 if (OPJ_IS_CINEMA(cp->rsiz)) {
564 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
565 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
571 distoachieved = layno == 0 ?
572 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
574 if (distoachieved < distotarget) {
576 stable_thresh = thresh;
583 distoachieved = (layno == 0) ?
584 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
586 if (distoachieved < distotarget) {
588 stable_thresh = thresh;
594 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
595 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
597 /* TODO: what to do with l ??? seek / tell ??? */
598 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
604 stable_thresh = thresh;
608 goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
615 if (cstr_info) { /* Threshold for Marcela Index */
616 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
619 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
622 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
623 (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
629 OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
630 opj_image_t * p_image,
632 opj_thread_pool_t* p_tp)
634 p_tcd->image = p_image;
637 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
638 sizeof(opj_tcd_tile_t));
639 if (! p_tcd->tcd_image->tiles) {
643 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
644 p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
645 if (! p_tcd->tcd_image->tiles->comps) {
649 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
650 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
651 p_tcd->thread_pool = p_tp;
657 Destroy a previously created TCD handle
659 void opj_tcd_destroy(opj_tcd_t *tcd)
662 opj_tcd_free_tile(tcd);
664 if (tcd->tcd_image) {
665 opj_free(tcd->tcd_image);
672 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
674 if ((l_tilec->data == 00) ||
675 ((l_tilec->data_size_needed > l_tilec->data_size) &&
676 (l_tilec->ownsData == OPJ_FALSE))) {
677 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
678 if (! l_tilec->data) {
681 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
682 l_tilec->data_size = l_tilec->data_size_needed;
683 l_tilec->ownsData = OPJ_TRUE;
684 } else if (l_tilec->data_size_needed > l_tilec->data_size) {
685 /* We don't need to keep old data */
686 opj_image_data_free(l_tilec->data);
687 l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
688 if (! l_tilec->data) {
689 l_tilec->data_size = 0;
690 l_tilec->data_size_needed = 0;
691 l_tilec->ownsData = OPJ_FALSE;
694 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
695 l_tilec->data_size = l_tilec->data_size_needed;
696 l_tilec->ownsData = OPJ_TRUE;
701 /* ----------------------------------------------------------------------- */
703 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
704 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
705 opj_event_mgr_t* manager)
707 OPJ_UINT32(*l_gain_ptr)(OPJ_UINT32) = 00;
708 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
709 opj_tcp_t * l_tcp = 00;
710 opj_cp_t * l_cp = 00;
711 opj_tcd_tile_t * l_tile = 00;
712 opj_tccp_t *l_tccp = 00;
713 opj_tcd_tilecomp_t *l_tilec = 00;
714 opj_image_comp_t * l_image_comp = 00;
715 opj_tcd_resolution_t *l_res = 00;
716 opj_tcd_band_t *l_band = 00;
717 opj_stepsize_t * l_step_size = 00;
718 opj_tcd_precinct_t *l_current_precinct = 00;
719 opj_image_t *l_image = 00;
721 OPJ_UINT32 l_level_no;
722 OPJ_UINT32 l_pdx, l_pdy;
724 OPJ_INT32 l_x0b, l_y0b;
725 OPJ_UINT32 l_tx0, l_ty0;
726 /* extent of precincts , top left, bottom right**/
727 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
728 /* number of precinct for a resolution */
729 OPJ_UINT32 l_nb_precincts;
730 /* room needed to store l_nb_precinct precinct for a resolution */
731 OPJ_UINT32 l_nb_precinct_size;
732 /* number of code blocks for a precinct*/
733 OPJ_UINT32 l_nb_code_blocks;
734 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
735 OPJ_UINT32 l_nb_code_blocks_size;
736 /* size of data for a tile */
737 OPJ_UINT32 l_data_size;
740 l_tcp = &(l_cp->tcps[p_tile_no]);
741 l_tile = p_tcd->tcd_image->tiles;
742 l_tccp = l_tcp->tccps;
743 l_tilec = l_tile->comps;
744 l_image = p_tcd->image;
745 l_image_comp = p_tcd->image->comps;
747 p = p_tile_no % l_cp->tw; /* tile coordinates */
748 q = p_tile_no / l_cp->tw;
749 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
751 /* 4 borders of the tile rescale on the image if necessary */
752 l_tx0 = l_cp->tx0 + p *
753 l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
754 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
755 l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
757 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
758 if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
759 opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
762 l_ty0 = l_cp->ty0 + q *
763 l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
764 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
765 l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
767 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
768 if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
769 opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
774 /* testcase 1888.pdf.asan.35.988 */
775 if (l_tccp->numresolutions == 0) {
776 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
779 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
781 /*tile->numcomps = image->numcomps; */
782 for (compno = 0; compno < l_tile->numcomps; ++compno) {
783 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
784 l_image_comp->resno_decoded = 0;
785 /* border of each l_tile component (global) */
786 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
787 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
788 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
789 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
790 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
792 /* compute l_data_size with overflow check */
793 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
794 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
795 if ((l_data_size > 0U) &&
796 ((((OPJ_UINT32) - 1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 -
798 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
801 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
803 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
804 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
807 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
808 l_tilec->numresolutions = l_tccp->numresolutions;
809 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
810 l_tilec->minimum_num_resolutions = 1;
812 l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
813 l_cp->m_specific_param.m_dec.m_reduce;
816 l_tilec->data_size_needed = l_data_size;
817 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
818 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
822 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
823 opj_tcd_resolution_t);
825 if (l_tilec->resolutions == 00) {
826 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
827 if (! l_tilec->resolutions) {
830 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
831 l_tilec->resolutions_size = l_data_size;
832 memset(l_tilec->resolutions, 0, l_data_size);
833 } else if (l_data_size > l_tilec->resolutions_size) {
834 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
835 l_tilec->resolutions, l_data_size);
836 if (! new_resolutions) {
837 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
838 opj_free(l_tilec->resolutions);
839 l_tilec->resolutions = NULL;
840 l_tilec->resolutions_size = 0;
843 l_tilec->resolutions = new_resolutions;
844 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
845 memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
846 l_data_size - l_tilec->resolutions_size);
847 l_tilec->resolutions_size = l_data_size;
850 l_level_no = l_tilec->numresolutions;
851 l_res = l_tilec->resolutions;
852 l_step_size = l_tccp->stepsizes;
853 if (l_tccp->qmfbid == 0) {
854 l_gain_ptr = &opj_dwt_getgain_real;
856 l_gain_ptr = &opj_dwt_getgain;
858 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
860 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
861 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
862 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
863 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
864 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
868 /* border for each resolution level (global) */
869 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
870 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
871 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
872 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
873 /*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);*/
874 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
875 l_pdx = l_tccp->prcw[resno];
876 l_pdy = l_tccp->prch[resno];
877 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
878 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
879 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
880 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
881 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
882 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
883 /*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 );*/
885 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
886 l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
887 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
888 l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
889 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
891 if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
892 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
895 l_nb_precincts = l_res->pw * l_res->ph;
897 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
899 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
902 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
905 tlcbgxstart = l_tl_prc_x_start;
906 tlcbgystart = l_tl_prc_y_start;
907 /*brcbgxend = l_br_prc_x_end;*/
908 /* brcbgyend = l_br_prc_y_end;*/
909 cbgwidthexpn = l_pdx;
910 cbgheightexpn = l_pdy;
913 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
914 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
915 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
916 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
917 cbgwidthexpn = l_pdx - 1;
918 cbgheightexpn = l_pdy - 1;
922 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
923 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
924 l_band = l_res->bands;
926 for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
928 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
932 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
933 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
934 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
935 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
937 l_band->bandno = bandno + 1;
938 /* x0b = 1 if bandno = 1 or 3 */
939 l_x0b = l_band->bandno & 1;
940 /* y0b = 1 if bandno = 2 or 3 */
941 l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
942 /* l_band border (global) */
943 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
944 l_level_no), (OPJ_INT32)(l_level_no + 1));
945 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
946 l_level_no), (OPJ_INT32)(l_level_no + 1));
947 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
948 l_level_no), (OPJ_INT32)(l_level_no + 1));
949 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
950 l_level_no), (OPJ_INT32)(l_level_no + 1));
954 /* Skip empty bands */
955 if (opj_tcd_is_band_empty(l_band)) {
956 /* Do not zero l_band->precints to avoid leaks */
957 /* but make sure we don't use it later, since */
958 /* it will point to precincts of previous bands... */
963 /** avoid an if with storing function pointer */
964 l_gain = (*l_gain_ptr)(l_band->bandno);
965 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
966 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
967 (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
968 /* Mb value of Equation E-2 in "E.1 Inverse quantization
969 * procedure" of the standard */
970 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
973 if (!l_band->precincts && (l_nb_precincts > 0U)) {
974 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
976 if (! l_band->precincts) {
977 opj_event_msg(manager, EVT_ERROR,
978 "Not enough memory to handle band precints\n");
981 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
982 memset(l_band->precincts, 0, l_nb_precinct_size);
983 l_band->precincts_data_size = l_nb_precinct_size;
984 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
986 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
987 l_band->precincts,/*3 * */ l_nb_precinct_size);
988 if (! new_precincts) {
989 opj_event_msg(manager, EVT_ERROR,
990 "Not enough memory to handle band precints\n");
991 opj_free(l_band->precincts);
992 l_band->precincts = NULL;
993 l_band->precincts_data_size = 0;
996 l_band->precincts = new_precincts;
997 /*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);*/
998 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
999 l_nb_precinct_size - l_band->precincts_data_size);
1000 l_band->precincts_data_size = l_nb_precinct_size;
1003 l_current_precinct = l_band->precincts;
1004 for (precno = 0; precno < l_nb_precincts; ++precno) {
1005 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1006 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1007 (1 << cbgwidthexpn);
1008 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1009 (1 << cbgheightexpn);
1010 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1011 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1012 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1013 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1015 /* precinct size (global) */
1016 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1018 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1019 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1020 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1021 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1022 /*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);*/
1024 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1025 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1026 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1027 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1028 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1029 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1030 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1031 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1032 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1033 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1034 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1035 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1036 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1038 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1041 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1042 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
1043 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1045 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1046 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1047 if (! l_current_precinct->cblks.blocks) {
1050 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1052 memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1054 l_current_precinct->block_size = l_nb_code_blocks_size;
1055 } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1056 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1057 l_nb_code_blocks_size);
1059 opj_free(l_current_precinct->cblks.blocks);
1060 l_current_precinct->cblks.blocks = NULL;
1061 l_current_precinct->block_size = 0;
1062 opj_event_msg(manager, EVT_ERROR,
1063 "Not enough memory for current precinct codeblock element\n");
1066 l_current_precinct->cblks.blocks = new_blocks;
1067 /*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); */
1069 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1070 l_current_precinct->block_size
1072 , l_nb_code_blocks_size - l_current_precinct->block_size);
1074 l_current_precinct->block_size = l_nb_code_blocks_size;
1077 if (! l_current_precinct->incltree) {
1078 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1079 l_current_precinct->ch, manager);
1081 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1082 l_current_precinct->cw, l_current_precinct->ch, manager);
1085 if (! l_current_precinct->imsbtree) {
1086 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1087 l_current_precinct->ch, manager);
1089 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1090 l_current_precinct->cw, l_current_precinct->ch, manager);
1093 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1094 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1095 l_current_precinct->cw) * (1 << cblkwidthexpn);
1096 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1097 l_current_precinct->cw) * (1 << cblkheightexpn);
1098 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1099 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1102 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1104 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1107 /* code-block size (global) */
1108 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1109 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1110 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1111 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1113 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1117 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1119 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1122 /* code-block size (global) */
1123 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1124 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1125 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1126 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1129 ++l_current_precinct;
1141 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1142 opj_event_mgr_t* p_manager)
1144 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1145 sizeof(opj_tcd_cblk_enc_t), p_manager);
1148 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1149 opj_event_mgr_t* p_manager)
1151 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1152 sizeof(opj_tcd_cblk_dec_t), p_manager);
1156 * Allocates memory for an encoding code block (but not data memory).
1158 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1161 if (! p_code_block->layers) {
1162 /* no memset since data */
1163 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1164 sizeof(opj_tcd_layer_t));
1165 if (! p_code_block->layers) {
1169 if (! p_code_block->passes) {
1170 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1171 sizeof(opj_tcd_pass_t));
1172 if (! p_code_block->passes) {
1180 * Allocates data memory for an encoding code block.
1182 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1185 OPJ_UINT32 l_data_size;
1187 /* The +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1188 l_data_size = 1 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1189 (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1191 if (l_data_size > p_code_block->data_size) {
1192 if (p_code_block->data) {
1193 /* We refer to data - 1 since below we incremented it */
1194 opj_free(p_code_block->data - 1);
1196 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1197 if (! p_code_block->data) {
1198 p_code_block->data_size = 0U;
1201 p_code_block->data_size = l_data_size;
1203 /* We reserve the initial byte as a fake byte to a non-FF value */
1204 /* and increment the data pointer, so that opj_mqc_init_enc() */
1205 /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1207 p_code_block->data[0] = 0;
1208 p_code_block->data += 1; /*why +1 ?*/
1214 void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1216 opj_tcd_seg_data_chunk_t *l_chunks = seg->chunks;
1217 OPJ_UINT32 l_numchunksalloc = seg->numchunksalloc;
1218 memset(seg, 0, sizeof(opj_tcd_seg_t));
1219 seg->chunks = l_chunks;
1220 seg->numchunksalloc = l_numchunksalloc;
1224 * Allocates memory for a decoding code block.
1226 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1229 if (! p_code_block->segs) {
1231 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1232 sizeof(opj_tcd_seg_t));
1233 if (! p_code_block->segs) {
1236 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1238 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1239 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1242 opj_tcd_seg_t * l_segs = p_code_block->segs;
1243 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1246 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1247 p_code_block->segs = l_segs;
1248 p_code_block->m_current_max_segs = l_current_max_segs;
1249 for (i = 0; i < l_current_max_segs; ++i) {
1250 opj_tcd_reinit_segment(&l_segs[i]);
1257 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd)
1260 OPJ_UINT32 l_data_size = 0;
1261 opj_image_comp_t * l_img_comp = 00;
1262 opj_tcd_tilecomp_t * l_tile_comp = 00;
1263 opj_tcd_resolution_t * l_res = 00;
1264 OPJ_UINT32 l_size_comp, l_remaining;
1267 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1268 l_img_comp = p_tcd->image->comps;
1270 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1271 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1272 l_remaining = l_img_comp->prec & 7; /* (%8) */
1278 if (l_size_comp == 3) {
1282 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1283 l_temp = (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 -
1284 l_res->y0)); /* x1*y1 can't overflow */
1285 if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1288 l_temp *= l_size_comp;
1290 if (l_temp > UINT_MAX - l_data_size) {
1293 l_data_size += l_temp;
1301 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1302 OPJ_UINT32 p_tile_no,
1304 OPJ_UINT32 * p_data_written,
1305 OPJ_UINT32 p_max_length,
1306 opj_codestream_info_t *p_cstr_info)
1309 if (p_tcd->cur_tp_num == 0) {
1311 p_tcd->tcd_tileno = p_tile_no;
1312 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1314 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1316 OPJ_UINT32 l_num_packs = 0;
1318 opj_tcd_tilecomp_t *l_tilec_idx =
1319 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1320 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1322 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1323 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1325 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1326 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1328 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1329 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1330 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1332 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1333 size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs,
1334 sizeof(opj_packet_info_t));
1335 if (!p_cstr_info->tile[p_tile_no].packet) {
1336 /* FIXME event manager error callback */
1342 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1343 /*---------------TILE-------------------*/
1344 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1347 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1349 /* FIXME _ProfStart(PGROUP_MCT); */
1350 if (! opj_tcd_mct_encode(p_tcd)) {
1353 /* FIXME _ProfStop(PGROUP_MCT); */
1355 /* FIXME _ProfStart(PGROUP_DWT); */
1356 if (! opj_tcd_dwt_encode(p_tcd)) {
1359 /* FIXME _ProfStop(PGROUP_DWT); */
1361 /* FIXME _ProfStart(PGROUP_T1); */
1362 if (! opj_tcd_t1_encode(p_tcd)) {
1365 /* FIXME _ProfStop(PGROUP_T1); */
1367 /* FIXME _ProfStart(PGROUP_RATE); */
1368 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length, p_cstr_info)) {
1371 /* FIXME _ProfStop(PGROUP_RATE); */
1374 /*--------------TIER2------------------*/
1378 p_cstr_info->index_write = 1;
1380 /* FIXME _ProfStart(PGROUP_T2); */
1382 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1386 /* FIXME _ProfStop(PGROUP_T2); */
1388 /*---------------CLEAN-------------------*/
1393 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1395 OPJ_UINT32 p_max_length,
1396 OPJ_UINT32 p_tile_no,
1397 opj_codestream_index_t *p_cstr_index,
1398 opj_event_mgr_t *p_manager
1401 OPJ_UINT32 l_data_read;
1402 p_tcd->tcd_tileno = p_tile_no;
1403 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1405 #ifdef TODO_MSD /* FIXME */
1408 OPJ_UINT32 resno, compno, numprec = 0;
1409 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1410 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1411 opj_tccp_t *tccp = &tcp->tccps[compno];
1412 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1413 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1414 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1415 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1416 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1417 numprec += res_idx->pw * res_idx->ph;
1418 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1419 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1422 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1423 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1424 p_cstr_info->packno = 0;
1429 /*--------------TIER2------------------*/
1430 /* FIXME _ProfStart(PGROUP_T2); */
1432 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1436 /* FIXME _ProfStop(PGROUP_T2); */
1438 /*------------------TIER1-----------------*/
1440 /* FIXME _ProfStart(PGROUP_T1); */
1441 if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1444 /* FIXME _ProfStop(PGROUP_T1); */
1446 /*----------------DWT---------------------*/
1448 /* FIXME _ProfStart(PGROUP_DWT); */
1450 (! opj_tcd_dwt_decode(p_tcd)) {
1453 /* FIXME _ProfStop(PGROUP_DWT); */
1455 /*----------------MCT-------------------*/
1456 /* FIXME _ProfStart(PGROUP_MCT); */
1458 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1461 /* FIXME _ProfStop(PGROUP_MCT); */
1463 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1465 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1468 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1471 /*---------------TILE-------------------*/
1475 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1477 OPJ_UINT32 p_dest_length
1480 OPJ_UINT32 i, j, k, l_data_size = 0;
1481 opj_image_comp_t * l_img_comp = 00;
1482 opj_tcd_tilecomp_t * l_tilec = 00;
1483 opj_tcd_resolution_t * l_res;
1484 OPJ_UINT32 l_size_comp, l_remaining;
1485 OPJ_UINT32 l_stride, l_width, l_height;
1487 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1488 if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1492 l_tilec = p_tcd->tcd_image->tiles->comps;
1493 l_img_comp = p_tcd->image->comps;
1495 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1496 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1497 l_remaining = l_img_comp->prec & 7; /* (%8) */
1498 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1499 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1500 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1501 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1507 if (l_size_comp == 3) {
1511 switch (l_size_comp) {
1513 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1514 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1516 if (l_img_comp->sgnd) {
1517 for (j = 0; j < l_height; ++j) {
1518 for (k = 0; k < l_width; ++k) {
1519 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1521 l_src_ptr += l_stride;
1524 for (j = 0; j < l_height; ++j) {
1525 for (k = 0; k < l_width; ++k) {
1526 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1528 l_src_ptr += l_stride;
1532 p_dest = (OPJ_BYTE *)l_dest_ptr;
1536 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1537 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1539 if (l_img_comp->sgnd) {
1540 for (j = 0; j < l_height; ++j) {
1541 for (k = 0; k < l_width; ++k) {
1542 OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1543 memcpy(l_dest_ptr, &val, sizeof(val));
1546 l_src_ptr += l_stride;
1549 for (j = 0; j < l_height; ++j) {
1550 for (k = 0; k < l_width; ++k) {
1551 OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1552 memcpy(l_dest_ptr, &val, sizeof(val));
1555 l_src_ptr += l_stride;
1559 p_dest = (OPJ_BYTE*) l_dest_ptr;
1563 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1564 OPJ_INT32 * l_src_ptr = l_tilec->data;
1566 for (j = 0; j < l_height; ++j) {
1567 memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1568 l_dest_ptr += l_width;
1569 l_src_ptr += l_width + l_stride;
1572 p_dest = (OPJ_BYTE*) l_dest_ptr;
1587 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1589 OPJ_UINT32 compno, resno, bandno, precno;
1590 opj_tcd_tile_t *l_tile = 00;
1591 opj_tcd_tilecomp_t *l_tile_comp = 00;
1592 opj_tcd_resolution_t *l_res = 00;
1593 opj_tcd_band_t *l_band = 00;
1594 opj_tcd_precinct_t *l_precinct = 00;
1595 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1596 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1602 if (! p_tcd->tcd_image) {
1606 if (p_tcd->m_is_decoder) {
1607 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1609 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1612 l_tile = p_tcd->tcd_image->tiles;
1617 l_tile_comp = l_tile->comps;
1619 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1620 l_res = l_tile_comp->resolutions;
1623 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1624 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1625 l_band = l_res->bands;
1626 for (bandno = 0; bandno < 3; ++bandno) {
1627 l_precinct = l_band->precincts;
1630 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1631 for (precno = 0; precno < l_nb_precincts; ++precno) {
1632 opj_tgt_destroy(l_precinct->incltree);
1633 l_precinct->incltree = 00;
1634 opj_tgt_destroy(l_precinct->imsbtree);
1635 l_precinct->imsbtree = 00;
1636 (*l_tcd_code_block_deallocate)(l_precinct);
1640 opj_free(l_band->precincts);
1641 l_band->precincts = 00;
1648 opj_free(l_tile_comp->resolutions);
1649 l_tile_comp->resolutions = 00;
1652 if (l_tile_comp->ownsData && l_tile_comp->data) {
1653 opj_image_data_free(l_tile_comp->data);
1654 l_tile_comp->data = 00;
1655 l_tile_comp->ownsData = 0;
1656 l_tile_comp->data_size = 0;
1657 l_tile_comp->data_size_needed = 0;
1662 opj_free(l_tile->comps);
1664 opj_free(p_tcd->tcd_image->tiles);
1665 p_tcd->tcd_image->tiles = 00;
1669 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1670 OPJ_BYTE * p_src_data,
1671 OPJ_UINT32 * p_data_read,
1672 OPJ_UINT32 p_max_src_size,
1673 opj_codestream_index_t *p_cstr_index,
1674 opj_event_mgr_t *p_manager
1679 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1684 if (! opj_t2_decode_packets(
1687 p_tcd->tcd_image->tiles,
1693 opj_t2_destroy(l_t2);
1697 opj_t2_destroy(l_t2);
1699 /*---------------CLEAN-------------------*/
1703 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1706 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1707 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1708 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1709 volatile OPJ_BOOL ret = OPJ_TRUE;
1710 OPJ_BOOL check_pterm = OPJ_FALSE;
1711 opj_mutex_t* p_manager_mutex = NULL;
1713 p_manager_mutex = opj_mutex_create();
1715 /* Only enable PTERM check if we decode all layers */
1716 if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
1717 (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
1718 check_pterm = OPJ_TRUE;
1721 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1722 opj_t1_decode_cblks(p_tcd->thread_pool, &ret, l_tile_comp, l_tccp,
1723 p_manager, p_manager_mutex, check_pterm);
1731 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1732 if (p_manager_mutex) {
1733 opj_mutex_destroy(p_manager_mutex);
1739 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1742 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1743 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1744 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1745 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1747 for (compno = 0; compno < l_tile->numcomps; compno++) {
1749 if (tcd->cp->reduce != 0) {
1750 tcd->image->comps[compno].resno_decoded =
1751 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1752 if (tcd->image->comps[compno].resno_decoded < 0)
1757 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1758 if(numres2decode > 0){
1761 if (l_tccp->qmfbid == 1) {
1762 if (! opj_dwt_decode(p_tcd->thread_pool, l_tile_comp,
1763 l_img_comp->resno_decoded + 1)) {
1767 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded + 1)) {
1779 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1781 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1782 opj_tcp_t * l_tcp = p_tcd->tcp;
1783 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1784 OPJ_UINT32 l_samples, i;
1790 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1791 (l_tile_comp->y1 - l_tile_comp->y0));
1793 if (l_tile->numcomps >= 3) {
1794 /* testcase 1336.pdf.asan.47.376 */
1795 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 -
1796 l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1797 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 -
1798 l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1799 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 -
1800 l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1801 opj_event_msg(p_manager, EVT_ERROR,
1802 "Tiles don't all have the same dimension. Skip the MCT step.\n");
1804 } else if (l_tcp->mct == 2) {
1807 if (! l_tcp->m_mct_decoding_matrix) {
1811 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
1816 for (i = 0; i < l_tile->numcomps; ++i) {
1817 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1821 if (! opj_mct_decode_custom(/* MCT data */
1822 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1823 /* size of components */
1827 /* nb of components (i.e. size of pData) */
1829 /* tells if the data is signed */
1830 p_tcd->image->comps->sgnd)) {
1837 if (l_tcp->tccps->qmfbid == 1) {
1838 opj_mct_decode(l_tile->comps[0].data,
1839 l_tile->comps[1].data,
1840 l_tile->comps[2].data,
1843 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1844 (OPJ_FLOAT32*)l_tile->comps[1].data,
1845 (OPJ_FLOAT32*)l_tile->comps[2].data,
1850 opj_event_msg(p_manager, EVT_ERROR,
1851 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
1859 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
1862 opj_tcd_tilecomp_t * l_tile_comp = 00;
1863 opj_tccp_t * l_tccp = 00;
1864 opj_image_comp_t * l_img_comp = 00;
1865 opj_tcd_resolution_t* l_res = 00;
1866 opj_tcd_tile_t * l_tile;
1867 OPJ_UINT32 l_width, l_height, i, j;
1868 OPJ_INT32 * l_current_ptr;
1869 OPJ_INT32 l_min, l_max;
1870 OPJ_UINT32 l_stride;
1872 l_tile = p_tcd->tcd_image->tiles;
1873 l_tile_comp = l_tile->comps;
1874 l_tccp = p_tcd->tcp->tccps;
1875 l_img_comp = p_tcd->image->comps;
1877 for (compno = 0; compno < l_tile->numcomps; compno++) {
1878 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1879 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1880 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1881 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1883 assert(l_height == 0 ||
1884 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1886 if (l_img_comp->sgnd) {
1887 l_min = -(1 << (l_img_comp->prec - 1));
1888 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1891 l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
1894 l_current_ptr = l_tile_comp->data;
1896 if (l_tccp->qmfbid == 1) {
1897 for (j = 0; j < l_height; ++j) {
1898 for (i = 0; i < l_width; ++i) {
1899 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
1903 l_current_ptr += l_stride;
1906 for (j = 0; j < l_height; ++j) {
1907 for (i = 0; i < l_width; ++i) {
1908 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1909 OPJ_INT32 l_value_int = (OPJ_INT32)opj_lrintf(l_value);
1910 if (l_value > INT_MAX ||
1911 (l_value_int > 0 && l_tccp->m_dc_level_shift > 0 &&
1912 l_value_int > INT_MAX - l_tccp->m_dc_level_shift)) {
1913 *l_current_ptr = l_max;
1915 *l_current_ptr = opj_int_clamp(
1916 l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
1920 l_current_ptr += l_stride;
1935 * Deallocates the encoding data of the given precinct.
1937 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
1939 OPJ_UINT32 cblkno, l_nb_code_blocks;
1941 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1943 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1944 /*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);*/
1945 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1946 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 );*/
1949 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1950 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1952 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1954 if (l_code_block->segs) {
1956 for (i = 0; i < l_code_block->m_current_max_segs; ++ i) {
1957 opj_free(l_code_block->segs[i].chunks);
1959 opj_free(l_code_block->segs);
1960 l_code_block->segs = 00;
1966 opj_free(p_precinct->cblks.dec);
1967 p_precinct->cblks.dec = 00;
1972 * Deallocates the encoding data of the given precinct.
1974 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
1976 OPJ_UINT32 cblkno, l_nb_code_blocks;
1978 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1980 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1982 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1983 if (l_code_block->data) {
1984 /* We refer to data - 1 since below we incremented it */
1985 /* in opj_tcd_code_block_enc_allocate_data() */
1986 opj_free(l_code_block->data - 1);
1987 l_code_block->data = 00;
1990 if (l_code_block->layers) {
1991 opj_free(l_code_block->layers);
1992 l_code_block->layers = 00;
1995 if (l_code_block->passes) {
1996 opj_free(l_code_block->passes);
1997 l_code_block->passes = 00;
2002 opj_free(p_precinct->cblks.enc);
2004 p_precinct->cblks.enc = 00;
2008 OPJ_UINT32 opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
2010 OPJ_UINT32 i, l_data_size = 0;
2011 opj_image_comp_t * l_img_comp = 00;
2012 opj_tcd_tilecomp_t * l_tilec = 00;
2013 OPJ_UINT32 l_size_comp, l_remaining;
2015 l_tilec = p_tcd->tcd_image->tiles->comps;
2016 l_img_comp = p_tcd->image->comps;
2017 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2018 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2019 l_remaining = l_img_comp->prec & 7; /* (%8) */
2025 if (l_size_comp == 3) {
2029 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) *
2030 (l_tilec->y1 - l_tilec->y0));
2038 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2041 opj_tcd_tilecomp_t * l_tile_comp = 00;
2042 opj_tccp_t * l_tccp = 00;
2043 opj_image_comp_t * l_img_comp = 00;
2044 opj_tcd_tile_t * l_tile;
2045 OPJ_UINT32 l_nb_elem, i;
2046 OPJ_INT32 * l_current_ptr;
2048 l_tile = p_tcd->tcd_image->tiles;
2049 l_tile_comp = l_tile->comps;
2050 l_tccp = p_tcd->tcp->tccps;
2051 l_img_comp = p_tcd->image->comps;
2053 for (compno = 0; compno < l_tile->numcomps; compno++) {
2054 l_current_ptr = l_tile_comp->data;
2055 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2056 (l_tile_comp->y1 - l_tile_comp->y0));
2058 if (l_tccp->qmfbid == 1) {
2059 for (i = 0; i < l_nb_elem; ++i) {
2060 *l_current_ptr -= l_tccp->m_dc_level_shift ;
2064 for (i = 0; i < l_nb_elem; ++i) {
2065 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2078 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2080 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2081 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2082 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2083 (l_tile_comp->y1 - l_tile_comp->y0));
2085 OPJ_BYTE ** l_data = 00;
2086 opj_tcp_t * l_tcp = p_tcd->tcp;
2088 if (!p_tcd->tcp->mct) {
2092 if (p_tcd->tcp->mct == 2) {
2093 if (! p_tcd->tcp->m_mct_coding_matrix) {
2097 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2102 for (i = 0; i < l_tile->numcomps; ++i) {
2103 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2107 if (! opj_mct_encode_custom(/* MCT data */
2108 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2109 /* size of components */
2113 /* nb of components (i.e. size of pData) */
2115 /* tells if the data is signed */
2116 p_tcd->image->comps->sgnd)) {
2122 } else if (l_tcp->tccps->qmfbid == 0) {
2123 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2124 l_tile->comps[2].data, samples);
2126 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2127 l_tile->comps[2].data, samples);
2133 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2135 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2136 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2137 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2140 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2141 if (l_tccp->qmfbid == 1) {
2142 if (! opj_dwt_encode(l_tile_comp)) {
2145 } else if (l_tccp->qmfbid == 0) {
2146 if (! opj_dwt_encode_real(l_tile_comp)) {
2158 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2161 const OPJ_FLOAT64 * l_mct_norms;
2162 OPJ_UINT32 l_mct_numcomps = 0U;
2163 opj_tcp_t * l_tcp = p_tcd->tcp;
2165 l_t1 = opj_t1_create(OPJ_TRUE);
2170 if (l_tcp->mct == 1) {
2171 l_mct_numcomps = 3U;
2172 /* irreversible encoding */
2173 if (l_tcp->tccps->qmfbid == 0) {
2174 l_mct_norms = opj_mct_get_mct_norms_real();
2176 l_mct_norms = opj_mct_get_mct_norms();
2179 l_mct_numcomps = p_tcd->image->numcomps;
2180 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2183 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2185 opj_t1_destroy(l_t1);
2189 opj_t1_destroy(l_t1);
2194 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2195 OPJ_BYTE * p_dest_data,
2196 OPJ_UINT32 * p_data_written,
2197 OPJ_UINT32 p_max_dest_size,
2198 opj_codestream_info_t *p_cstr_info)
2202 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2207 if (! opj_t2_encode_packets(
2210 p_tcd->tcd_image->tiles,
2211 p_tcd->tcp->numlayers,
2220 opj_t2_destroy(l_t2);
2224 opj_t2_destroy(l_t2);
2226 /*---------------CLEAN-------------------*/
2231 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2232 OPJ_BYTE * p_dest_data,
2233 OPJ_UINT32 p_max_dest_size,
2234 opj_codestream_info_t *p_cstr_info)
2236 opj_cp_t * l_cp = p_tcd->cp;
2237 OPJ_UINT32 l_nb_written = 0;
2240 p_cstr_info->index_write = 0;
2243 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2244 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2246 /* Normal Rate/distortion allocation */
2247 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2252 /* Fixed layer allocation */
2253 opj_tcd_rateallocate_fixed(p_tcd);
2260 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2262 OPJ_UINT32 p_src_length)
2264 OPJ_UINT32 i, j, l_data_size = 0;
2265 opj_image_comp_t * l_img_comp = 00;
2266 opj_tcd_tilecomp_t * l_tilec = 00;
2267 OPJ_UINT32 l_size_comp, l_remaining;
2268 OPJ_UINT32 l_nb_elem;
2270 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2271 if (l_data_size != p_src_length) {
2275 l_tilec = p_tcd->tcd_image->tiles->comps;
2276 l_img_comp = p_tcd->image->comps;
2277 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2278 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2279 l_remaining = l_img_comp->prec & 7; /* (%8) */
2280 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 -
2287 if (l_size_comp == 3) {
2291 switch (l_size_comp) {
2293 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2294 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2296 if (l_img_comp->sgnd) {
2297 for (j = 0; j < l_nb_elem; ++j) {
2298 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2301 for (j = 0; j < l_nb_elem; ++j) {
2302 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2306 p_src = (OPJ_BYTE*) l_src_ptr;
2310 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2311 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2313 if (l_img_comp->sgnd) {
2314 for (j = 0; j < l_nb_elem; ++j) {
2315 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2318 for (j = 0; j < l_nb_elem; ++j) {
2319 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2323 p_src = (OPJ_BYTE*) l_src_ptr;
2327 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2328 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2330 for (j = 0; j < l_nb_elem; ++j) {
2331 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2334 p_src = (OPJ_BYTE*) l_src_ptr;
2346 OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2348 return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);