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"
43 /* ----------------------------------------------------------------------- */
47 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
49 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
51 fprintf(fd, "image {\n");
52 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
53 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
56 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
57 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
58 fprintf(fd, " tile {\n");
59 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
60 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
61 for (compno = 0; compno < tile->numcomps; compno++) {
62 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
63 fprintf(fd, " tilec {\n");
65 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
66 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
67 for (resno = 0; resno < tilec->numresolutions; resno++) {
68 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
69 fprintf(fd, "\n res {\n");
71 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
72 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
73 for (bandno = 0; bandno < res->numbands; bandno++) {
74 opj_tcd_band_t *band = &res->bands[bandno];
75 fprintf(fd, " band {\n");
77 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
78 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
79 for (precno = 0; precno < res->pw * res->ph; precno++) {
80 opj_tcd_precinct_t *prec = &band->precincts[precno];
81 fprintf(fd, " prec {\n");
83 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
84 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
86 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
87 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
88 fprintf(fd, " cblk {\n");
90 " x0=%d, y0=%d, x1=%d, y1=%d\n",
91 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
110 * Initializes tile coding/decoding
112 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
113 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
114 opj_event_mgr_t* manager);
117 * Allocates memory for a decoding code block.
119 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
123 * Deallocates the decoding data of the given precinct.
125 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct);
128 * Allocates memory for an encoding code block (but not data).
130 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
134 * Allocates data for an encoding code block
136 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
140 * Deallocates the encoding data of the given precinct.
142 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct);
146 Free the memory allocated for encoding
147 @param tcd TCD handle
149 static void opj_tcd_free_tile(opj_tcd_t *tcd);
152 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
153 OPJ_BYTE * p_src_data,
154 OPJ_UINT32 * p_data_read,
155 OPJ_UINT32 p_max_src_size,
156 opj_codestream_index_t *p_cstr_index,
157 opj_event_mgr_t *p_manager);
159 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd);
161 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd);
163 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd,
164 opj_event_mgr_t *p_manager);
166 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd);
169 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd);
171 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd);
173 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd);
175 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd);
177 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
178 OPJ_BYTE * p_dest_data,
179 OPJ_UINT32 * p_data_written,
180 OPJ_UINT32 p_max_dest_size,
181 opj_codestream_info_t *p_cstr_info);
183 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
184 OPJ_BYTE * p_dest_data,
185 OPJ_UINT32 p_max_dest_size,
186 opj_codestream_info_t *p_cstr_info);
188 /* ----------------------------------------------------------------------- */
191 Create a new TCD handle
193 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
195 opj_tcd_t *l_tcd = 00;
197 /* create the tcd structure */
198 l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
203 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
205 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
206 if (!l_tcd->tcd_image) {
215 /* ----------------------------------------------------------------------- */
217 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
221 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
222 opj_tcd_makelayer_fixed(tcd, layno, 1);
227 void opj_tcd_makelayer(opj_tcd_t *tcd,
232 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
235 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
237 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
239 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
240 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
242 for (resno = 0; resno < tilec->numresolutions; resno++) {
243 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
245 for (bandno = 0; bandno < res->numbands; bandno++) {
246 opj_tcd_band_t *band = &res->bands[bandno];
248 for (precno = 0; precno < res->pw * res->ph; precno++) {
249 opj_tcd_precinct_t *prc = &band->precincts[precno];
251 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
252 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
253 opj_tcd_layer_t *layer = &cblk->layers[layno];
257 cblk->numpassesinlayers = 0;
260 n = cblk->numpassesinlayers;
262 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
265 opj_tcd_pass_t *pass = &cblk->passes[passno];
269 dd = pass->distortiondec;
271 dr = pass->rate - cblk->passes[n - 1].rate;
272 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
281 if (thresh - (dd / dr) <
282 DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
287 layer->numpasses = n - cblk->numpassesinlayers;
289 if (!layer->numpasses) {
294 if (cblk->numpassesinlayers == 0) {
295 layer->len = cblk->passes[n - 1].rate;
296 layer->data = cblk->data;
297 layer->disto = cblk->passes[n - 1].distortiondec;
299 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
301 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
302 layer->disto = cblk->passes[n - 1].distortiondec -
303 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
306 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
309 cblk->numpassesinlayers = n;
318 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
321 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
322 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
323 OPJ_INT32 matrice[10][10][3];
326 opj_cp_t *cp = tcd->cp;
327 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
328 opj_tcp_t *tcd_tcp = tcd->tcp;
330 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
331 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
333 for (i = 0; i < tcd_tcp->numlayers; i++) {
334 for (j = 0; j < tilec->numresolutions; j++) {
335 for (k = 0; k < 3; k++) {
337 (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
338 tilec->numresolutions * 3 + j * 3 + k]
339 * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
344 for (resno = 0; resno < tilec->numresolutions; resno++) {
345 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
347 for (bandno = 0; bandno < res->numbands; bandno++) {
348 opj_tcd_band_t *band = &res->bands[bandno];
350 for (precno = 0; precno < res->pw * res->ph; precno++) {
351 opj_tcd_precinct_t *prc = &band->precincts[precno];
353 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
354 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
355 opj_tcd_layer_t *layer = &cblk->layers[layno];
357 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
358 cblk->numbps); /* number of bit-plan equal to zero */
360 /* Correction of the matrix of coefficient to include the IMSB information */
362 value = matrice[layno][resno][bandno];
369 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
370 if (imsb >= matrice[layno - 1][resno][bandno]) {
371 value -= (imsb - matrice[layno - 1][resno][bandno]);
379 cblk->numpassesinlayers = 0;
382 n = cblk->numpassesinlayers;
383 if (cblk->numpassesinlayers == 0) {
385 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
387 n = cblk->numpassesinlayers;
390 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
393 layer->numpasses = n - cblk->numpassesinlayers;
395 if (!layer->numpasses) {
399 if (cblk->numpassesinlayers == 0) {
400 layer->len = cblk->passes[n - 1].rate;
401 layer->data = cblk->data;
403 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
405 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
409 cblk->numpassesinlayers = n;
418 OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
420 OPJ_UINT32 * p_data_written,
422 opj_codestream_info_t *cstr_info)
424 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
426 OPJ_FLOAT64 min, max;
427 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
428 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
429 OPJ_FLOAT64 maxSE = 0;
431 opj_cp_t *cp = tcd->cp;
432 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
433 opj_tcp_t *tcd_tcp = tcd->tcp;
438 tcd_tile->numpix = 0; /* fixed_quality */
440 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
441 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
444 for (resno = 0; resno < tilec->numresolutions; resno++) {
445 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
447 for (bandno = 0; bandno < res->numbands; bandno++) {
448 opj_tcd_band_t *band = &res->bands[bandno];
450 for (precno = 0; precno < res->pw * res->ph; precno++) {
451 opj_tcd_precinct_t *prc = &band->precincts[precno];
453 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
454 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
456 for (passno = 0; passno < cblk->totalpasses; passno++) {
457 opj_tcd_pass_t *pass = &cblk->passes[passno];
459 OPJ_FLOAT64 dd, rdslope;
462 dr = (OPJ_INT32)pass->rate;
463 dd = pass->distortiondec;
465 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
466 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
484 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
485 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
491 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
492 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
493 * ((OPJ_FLOAT64)(tilec->numpix));
498 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
499 tile_info->numpix = tcd_tile->numpix;
500 tile_info->distotile = tcd_tile->distotile;
501 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
503 if (!tile_info->thresh) {
504 /* FIXME event manager error callback */
509 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
510 OPJ_FLOAT64 lo = min;
511 OPJ_FLOAT64 hi = max;
512 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
513 OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
514 OPJ_FLOAT64 goodthresh = 0;
515 OPJ_FLOAT64 stable_thresh = 0;
517 OPJ_FLOAT64 distotarget; /* fixed_quality */
520 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
521 tcd_tcp->distoratio[layno] / 10));
523 /* Don't try to find an optimal threshold but rather take everything not included yet, if
524 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
525 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
526 ==> possible to have some lossy layers and the last layer for sure lossless */
527 if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
528 (tcd_tcp->rates[layno] > 0.0f)) ||
529 ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
530 (tcd_tcp->distoratio[layno] > 0.0))) {
531 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
532 OPJ_FLOAT64 thresh = 0;
538 for (i = 0; i < 128; ++i) {
539 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
541 thresh = (lo + hi) / 2;
543 opj_tcd_makelayer(tcd, layno, thresh, 0);
545 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
546 if (OPJ_IS_CINEMA(cp->rsiz)) {
547 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
548 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
554 distoachieved = layno == 0 ?
555 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
557 if (distoachieved < distotarget) {
559 stable_thresh = thresh;
566 distoachieved = (layno == 0) ?
567 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
569 if (distoachieved < distotarget) {
571 stable_thresh = thresh;
577 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
578 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
580 /* TODO: what to do with l ??? seek / tell ??? */
581 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
587 stable_thresh = thresh;
591 goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
598 if (cstr_info) { /* Threshold for Marcela Index */
599 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
602 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
605 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
606 (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
612 OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
613 opj_image_t * p_image,
615 opj_thread_pool_t* p_tp)
617 p_tcd->image = p_image;
620 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
621 sizeof(opj_tcd_tile_t));
622 if (! p_tcd->tcd_image->tiles) {
626 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
627 p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
628 if (! p_tcd->tcd_image->tiles->comps) {
632 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
633 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
634 p_tcd->thread_pool = p_tp;
640 Destroy a previously created TCD handle
642 void opj_tcd_destroy(opj_tcd_t *tcd)
645 opj_tcd_free_tile(tcd);
647 if (tcd->tcd_image) {
648 opj_free(tcd->tcd_image);
655 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
657 if ((l_tilec->data == 00) ||
658 ((l_tilec->data_size_needed > l_tilec->data_size) &&
659 (l_tilec->ownsData == OPJ_FALSE))) {
660 l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed);
661 if (! l_tilec->data) {
664 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
665 l_tilec->data_size = l_tilec->data_size_needed;
666 l_tilec->ownsData = OPJ_TRUE;
667 } else if (l_tilec->data_size_needed > l_tilec->data_size) {
668 /* We don't need to keep old data */
669 opj_aligned_free(l_tilec->data);
670 l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed);
671 if (! l_tilec->data) {
672 l_tilec->data_size = 0;
673 l_tilec->data_size_needed = 0;
674 l_tilec->ownsData = OPJ_FALSE;
677 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
678 l_tilec->data_size = l_tilec->data_size_needed;
679 l_tilec->ownsData = OPJ_TRUE;
684 /* ----------------------------------------------------------------------- */
686 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
687 OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
688 opj_event_mgr_t* manager)
690 OPJ_UINT32(*l_gain_ptr)(OPJ_UINT32) = 00;
691 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
692 opj_tcp_t * l_tcp = 00;
693 opj_cp_t * l_cp = 00;
694 opj_tcd_tile_t * l_tile = 00;
695 opj_tccp_t *l_tccp = 00;
696 opj_tcd_tilecomp_t *l_tilec = 00;
697 opj_image_comp_t * l_image_comp = 00;
698 opj_tcd_resolution_t *l_res = 00;
699 opj_tcd_band_t *l_band = 00;
700 opj_stepsize_t * l_step_size = 00;
701 opj_tcd_precinct_t *l_current_precinct = 00;
702 opj_image_t *l_image = 00;
704 OPJ_UINT32 l_level_no;
705 OPJ_UINT32 l_pdx, l_pdy;
707 OPJ_INT32 l_x0b, l_y0b;
708 OPJ_UINT32 l_tx0, l_ty0;
709 /* extent of precincts , top left, bottom right**/
710 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
711 /* number of precinct for a resolution */
712 OPJ_UINT32 l_nb_precincts;
713 /* room needed to store l_nb_precinct precinct for a resolution */
714 OPJ_UINT32 l_nb_precinct_size;
715 /* number of code blocks for a precinct*/
716 OPJ_UINT32 l_nb_code_blocks;
717 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
718 OPJ_UINT32 l_nb_code_blocks_size;
719 /* size of data for a tile */
720 OPJ_UINT32 l_data_size;
723 l_tcp = &(l_cp->tcps[p_tile_no]);
724 l_tile = p_tcd->tcd_image->tiles;
725 l_tccp = l_tcp->tccps;
726 l_tilec = l_tile->comps;
727 l_image = p_tcd->image;
728 l_image_comp = p_tcd->image->comps;
730 p = p_tile_no % l_cp->tw; /* tile coordinates */
731 q = p_tile_no / l_cp->tw;
732 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
734 /* 4 borders of the tile rescale on the image if necessary */
735 l_tx0 = l_cp->tx0 + p *
736 l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
737 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
738 l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
740 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
741 if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
742 opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
745 l_ty0 = l_cp->ty0 + q *
746 l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
747 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
748 l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
750 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
751 if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
752 opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
757 /* testcase 1888.pdf.asan.35.988 */
758 if (l_tccp->numresolutions == 0) {
759 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
762 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
764 /*tile->numcomps = image->numcomps; */
765 for (compno = 0; compno < l_tile->numcomps; ++compno) {
766 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
767 l_image_comp->resno_decoded = 0;
768 /* border of each l_tile component (global) */
769 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
770 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
771 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
772 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
773 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
775 /* compute l_data_size with overflow check */
776 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
777 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
778 if ((l_data_size > 0U) &&
779 ((((OPJ_UINT32) - 1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 -
781 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
784 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
786 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
787 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
790 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
791 l_tilec->numresolutions = l_tccp->numresolutions;
792 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
793 l_tilec->minimum_num_resolutions = 1;
795 l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
796 l_cp->m_specific_param.m_dec.m_reduce;
799 l_tilec->data_size_needed = l_data_size;
800 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
801 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
805 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
806 opj_tcd_resolution_t);
808 if (l_tilec->resolutions == 00) {
809 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
810 if (! l_tilec->resolutions) {
813 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
814 l_tilec->resolutions_size = l_data_size;
815 memset(l_tilec->resolutions, 0, l_data_size);
816 } else if (l_data_size > l_tilec->resolutions_size) {
817 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
818 l_tilec->resolutions, l_data_size);
819 if (! new_resolutions) {
820 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
821 opj_free(l_tilec->resolutions);
822 l_tilec->resolutions = NULL;
823 l_tilec->resolutions_size = 0;
826 l_tilec->resolutions = new_resolutions;
827 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
828 memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
829 l_data_size - l_tilec->resolutions_size);
830 l_tilec->resolutions_size = l_data_size;
833 l_level_no = l_tilec->numresolutions;
834 l_res = l_tilec->resolutions;
835 l_step_size = l_tccp->stepsizes;
836 if (l_tccp->qmfbid == 0) {
837 l_gain_ptr = &opj_dwt_getgain_real;
839 l_gain_ptr = &opj_dwt_getgain;
841 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
843 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
844 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
845 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
846 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
847 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
851 /* border for each resolution level (global) */
852 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
853 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
854 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
855 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
856 /*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);*/
857 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
858 l_pdx = l_tccp->prcw[resno];
859 l_pdy = l_tccp->prch[resno];
860 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
861 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
862 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
863 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
864 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
865 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
866 /*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 );*/
868 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
869 l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
870 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
871 l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
872 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
874 if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
875 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
878 l_nb_precincts = l_res->pw * l_res->ph;
880 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
882 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
885 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
888 tlcbgxstart = l_tl_prc_x_start;
889 tlcbgystart = l_tl_prc_y_start;
890 /*brcbgxend = l_br_prc_x_end;*/
891 /* brcbgyend = l_br_prc_y_end;*/
892 cbgwidthexpn = l_pdx;
893 cbgheightexpn = l_pdy;
896 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
897 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
898 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
899 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
900 cbgwidthexpn = l_pdx - 1;
901 cbgheightexpn = l_pdy - 1;
905 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
906 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
907 l_band = l_res->bands;
909 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
911 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
915 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
916 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
917 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
918 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
920 l_band->bandno = bandno + 1;
921 /* x0b = 1 if bandno = 1 or 3 */
922 l_x0b = l_band->bandno & 1;
923 /* y0b = 1 if bandno = 2 or 3 */
924 l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
925 /* l_band border (global) */
926 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
927 l_level_no), (OPJ_INT32)(l_level_no + 1));
928 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
929 l_level_no), (OPJ_INT32)(l_level_no + 1));
930 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
931 l_level_no), (OPJ_INT32)(l_level_no + 1));
932 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
933 l_level_no), (OPJ_INT32)(l_level_no + 1));
936 /** avoid an if with storing function pointer */
937 l_gain = (*l_gain_ptr)(l_band->bandno);
938 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
939 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
940 (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
941 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
944 if (!l_band->precincts && (l_nb_precincts > 0U)) {
945 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
947 if (! l_band->precincts) {
948 opj_event_msg(manager, EVT_ERROR,
949 "Not enough memory to handle band precints\n");
952 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
953 memset(l_band->precincts, 0, l_nb_precinct_size);
954 l_band->precincts_data_size = l_nb_precinct_size;
955 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
957 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
958 l_band->precincts,/*3 * */ l_nb_precinct_size);
959 if (! new_precincts) {
960 opj_event_msg(manager, EVT_ERROR,
961 "Not enough memory to handle band precints\n");
962 opj_free(l_band->precincts);
963 l_band->precincts = NULL;
964 l_band->precincts_data_size = 0;
967 l_band->precincts = new_precincts;
968 /*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);*/
969 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
970 l_nb_precinct_size - l_band->precincts_data_size);
971 l_band->precincts_data_size = l_nb_precinct_size;
974 l_current_precinct = l_band->precincts;
975 for (precno = 0; precno < l_nb_precincts; ++precno) {
976 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
977 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
979 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
980 (1 << cbgheightexpn);
981 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
982 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
983 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
984 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
986 /* precinct size (global) */
987 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
989 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
990 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
991 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
992 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
993 /*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);*/
995 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
996 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
997 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
998 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
999 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1000 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1001 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1002 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1003 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1004 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1005 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1006 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1007 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1009 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1012 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1013 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
1014 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1016 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1017 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1018 if (! l_current_precinct->cblks.blocks) {
1021 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1023 memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1025 l_current_precinct->block_size = l_nb_code_blocks_size;
1026 } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1027 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1028 l_nb_code_blocks_size);
1030 opj_free(l_current_precinct->cblks.blocks);
1031 l_current_precinct->cblks.blocks = NULL;
1032 l_current_precinct->block_size = 0;
1033 opj_event_msg(manager, EVT_ERROR,
1034 "Not enough memory for current precinct codeblock element\n");
1037 l_current_precinct->cblks.blocks = new_blocks;
1038 /*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); */
1040 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1041 l_current_precinct->block_size
1043 , l_nb_code_blocks_size - l_current_precinct->block_size);
1045 l_current_precinct->block_size = l_nb_code_blocks_size;
1048 if (! l_current_precinct->incltree) {
1049 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1050 l_current_precinct->ch, manager);
1052 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1053 l_current_precinct->cw, l_current_precinct->ch, manager);
1056 if (! l_current_precinct->imsbtree) {
1057 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1058 l_current_precinct->ch, manager);
1060 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1061 l_current_precinct->cw, l_current_precinct->ch, manager);
1064 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1065 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1066 l_current_precinct->cw) * (1 << cblkwidthexpn);
1067 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1068 l_current_precinct->cw) * (1 << cblkheightexpn);
1069 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1070 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1073 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1075 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1078 /* code-block size (global) */
1079 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1080 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1081 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1082 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1084 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1088 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1090 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1093 /* code-block size (global) */
1094 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1095 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1096 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1097 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1100 ++l_current_precinct;
1114 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1115 opj_event_mgr_t* p_manager)
1117 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1118 sizeof(opj_tcd_cblk_enc_t), p_manager);
1121 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1122 opj_event_mgr_t* p_manager)
1124 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1125 sizeof(opj_tcd_cblk_dec_t), p_manager);
1129 * Allocates memory for an encoding code block (but not data memory).
1131 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1134 if (! p_code_block->layers) {
1135 /* no memset since data */
1136 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1137 sizeof(opj_tcd_layer_t));
1138 if (! p_code_block->layers) {
1142 if (! p_code_block->passes) {
1143 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1144 sizeof(opj_tcd_pass_t));
1145 if (! p_code_block->passes) {
1153 * Allocates data memory for an encoding code block.
1155 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1158 OPJ_UINT32 l_data_size;
1160 l_data_size = (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1161 (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1163 if (l_data_size > p_code_block->data_size) {
1164 if (p_code_block->data) {
1165 /* We refer to data - 1 since below we incremented it */
1166 opj_free(p_code_block->data - 1);
1168 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1169 if (! p_code_block->data) {
1170 p_code_block->data_size = 0U;
1173 p_code_block->data_size = l_data_size;
1175 /* We reserve the initial byte as a fake byte to a non-FF value */
1176 /* and increment the data pointer, so that opj_mqc_init_enc() */
1177 /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1179 p_code_block->data[0] = 0;
1180 p_code_block->data += 1; /*why +1 ?*/
1186 * Allocates memory for a decoding code block.
1188 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1191 if (! p_code_block->data) {
1193 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1194 if (! p_code_block->data) {
1197 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1198 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1200 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1201 sizeof(opj_tcd_seg_t));
1202 if (! p_code_block->segs) {
1205 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1207 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1208 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1211 OPJ_BYTE* l_data = p_code_block->data;
1212 OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1213 opj_tcd_seg_t * l_segs = p_code_block->segs;
1214 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1216 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1217 p_code_block->data = l_data;
1218 p_code_block->data_max_size = l_data_max_size;
1219 p_code_block->segs = l_segs;
1220 p_code_block->m_current_max_segs = l_current_max_segs;
1226 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd)
1229 OPJ_UINT32 l_data_size = 0;
1230 opj_image_comp_t * l_img_comp = 00;
1231 opj_tcd_tilecomp_t * l_tile_comp = 00;
1232 opj_tcd_resolution_t * l_res = 00;
1233 OPJ_UINT32 l_size_comp, l_remaining;
1235 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1236 l_img_comp = p_tcd->image->comps;
1238 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1239 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1240 l_remaining = l_img_comp->prec & 7; /* (%8) */
1246 if (l_size_comp == 3) {
1250 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1251 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) *
1252 (l_res->y1 - l_res->y0));
1260 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1261 OPJ_UINT32 p_tile_no,
1263 OPJ_UINT32 * p_data_written,
1264 OPJ_UINT32 p_max_length,
1265 opj_codestream_info_t *p_cstr_info)
1268 if (p_tcd->cur_tp_num == 0) {
1270 p_tcd->tcd_tileno = p_tile_no;
1271 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1273 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1275 OPJ_UINT32 l_num_packs = 0;
1277 opj_tcd_tilecomp_t *l_tilec_idx =
1278 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1279 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1281 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1282 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1284 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1285 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1287 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1288 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1289 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1291 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1292 size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs,
1293 sizeof(opj_packet_info_t));
1294 if (!p_cstr_info->tile[p_tile_no].packet) {
1295 /* FIXME event manager error callback */
1301 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1302 /*---------------TILE-------------------*/
1303 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1306 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1308 /* FIXME _ProfStart(PGROUP_MCT); */
1309 if (! opj_tcd_mct_encode(p_tcd)) {
1312 /* FIXME _ProfStop(PGROUP_MCT); */
1314 /* FIXME _ProfStart(PGROUP_DWT); */
1315 if (! opj_tcd_dwt_encode(p_tcd)) {
1318 /* FIXME _ProfStop(PGROUP_DWT); */
1320 /* FIXME _ProfStart(PGROUP_T1); */
1321 if (! opj_tcd_t1_encode(p_tcd)) {
1324 /* FIXME _ProfStop(PGROUP_T1); */
1326 /* FIXME _ProfStart(PGROUP_RATE); */
1327 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length, p_cstr_info)) {
1330 /* FIXME _ProfStop(PGROUP_RATE); */
1333 /*--------------TIER2------------------*/
1337 p_cstr_info->index_write = 1;
1339 /* FIXME _ProfStart(PGROUP_T2); */
1341 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1345 /* FIXME _ProfStop(PGROUP_T2); */
1347 /*---------------CLEAN-------------------*/
1352 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1354 OPJ_UINT32 p_max_length,
1355 OPJ_UINT32 p_tile_no,
1356 opj_codestream_index_t *p_cstr_index,
1357 opj_event_mgr_t *p_manager
1360 OPJ_UINT32 l_data_read;
1361 p_tcd->tcd_tileno = p_tile_no;
1362 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1364 #ifdef TODO_MSD /* FIXME */
1367 OPJ_UINT32 resno, compno, numprec = 0;
1368 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1369 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1370 opj_tccp_t *tccp = &tcp->tccps[compno];
1371 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1372 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1373 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1374 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1375 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1376 numprec += res_idx->pw * res_idx->ph;
1377 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1378 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1381 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1382 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1383 p_cstr_info->packno = 0;
1388 /*--------------TIER2------------------*/
1389 /* FIXME _ProfStart(PGROUP_T2); */
1391 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1395 /* FIXME _ProfStop(PGROUP_T2); */
1397 /*------------------TIER1-----------------*/
1399 /* FIXME _ProfStart(PGROUP_T1); */
1401 (! opj_tcd_t1_decode(p_tcd)) {
1404 /* FIXME _ProfStop(PGROUP_T1); */
1406 /*----------------DWT---------------------*/
1408 /* FIXME _ProfStart(PGROUP_DWT); */
1410 (! opj_tcd_dwt_decode(p_tcd)) {
1413 /* FIXME _ProfStop(PGROUP_DWT); */
1415 /*----------------MCT-------------------*/
1416 /* FIXME _ProfStart(PGROUP_MCT); */
1418 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1421 /* FIXME _ProfStop(PGROUP_MCT); */
1423 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1425 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1428 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1431 /*---------------TILE-------------------*/
1435 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1437 OPJ_UINT32 p_dest_length
1440 OPJ_UINT32 i, j, k, l_data_size = 0;
1441 opj_image_comp_t * l_img_comp = 00;
1442 opj_tcd_tilecomp_t * l_tilec = 00;
1443 opj_tcd_resolution_t * l_res;
1444 OPJ_UINT32 l_size_comp, l_remaining;
1445 OPJ_UINT32 l_stride, l_width, l_height;
1447 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1448 if (l_data_size > p_dest_length) {
1452 l_tilec = p_tcd->tcd_image->tiles->comps;
1453 l_img_comp = p_tcd->image->comps;
1455 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1456 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1457 l_remaining = l_img_comp->prec & 7; /* (%8) */
1458 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1459 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1460 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1461 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1467 if (l_size_comp == 3) {
1471 switch (l_size_comp) {
1473 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1474 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1476 if (l_img_comp->sgnd) {
1477 for (j = 0; j < l_height; ++j) {
1478 for (k = 0; k < l_width; ++k) {
1479 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1481 l_src_ptr += l_stride;
1484 for (j = 0; j < l_height; ++j) {
1485 for (k = 0; k < l_width; ++k) {
1486 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1488 l_src_ptr += l_stride;
1492 p_dest = (OPJ_BYTE *)l_dest_ptr;
1496 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1497 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1499 if (l_img_comp->sgnd) {
1500 for (j = 0; j < l_height; ++j) {
1501 for (k = 0; k < l_width; ++k) {
1502 *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++));
1504 l_src_ptr += l_stride;
1507 for (j = 0; j < l_height; ++j) {
1508 for (k = 0; k < l_width; ++k) {
1509 *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1511 l_src_ptr += l_stride;
1515 p_dest = (OPJ_BYTE*) l_dest_ptr;
1519 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1520 OPJ_INT32 * l_src_ptr = l_tilec->data;
1522 for (j = 0; j < l_height; ++j) {
1523 for (k = 0; k < l_width; ++k) {
1524 *(l_dest_ptr++) = (*(l_src_ptr++));
1526 l_src_ptr += l_stride;
1529 p_dest = (OPJ_BYTE*) l_dest_ptr;
1544 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1546 OPJ_UINT32 compno, resno, bandno, precno;
1547 opj_tcd_tile_t *l_tile = 00;
1548 opj_tcd_tilecomp_t *l_tile_comp = 00;
1549 opj_tcd_resolution_t *l_res = 00;
1550 opj_tcd_band_t *l_band = 00;
1551 opj_tcd_precinct_t *l_precinct = 00;
1552 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1553 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1559 if (! p_tcd->tcd_image) {
1563 if (p_tcd->m_is_decoder) {
1564 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1566 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1569 l_tile = p_tcd->tcd_image->tiles;
1574 l_tile_comp = l_tile->comps;
1576 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1577 l_res = l_tile_comp->resolutions;
1580 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1581 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1582 l_band = l_res->bands;
1583 for (bandno = 0; bandno < 3; ++bandno) {
1584 l_precinct = l_band->precincts;
1587 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1588 for (precno = 0; precno < l_nb_precincts; ++precno) {
1589 opj_tgt_destroy(l_precinct->incltree);
1590 l_precinct->incltree = 00;
1591 opj_tgt_destroy(l_precinct->imsbtree);
1592 l_precinct->imsbtree = 00;
1593 (*l_tcd_code_block_deallocate)(l_precinct);
1597 opj_free(l_band->precincts);
1598 l_band->precincts = 00;
1605 opj_free(l_tile_comp->resolutions);
1606 l_tile_comp->resolutions = 00;
1609 if (l_tile_comp->ownsData && l_tile_comp->data) {
1610 opj_aligned_free(l_tile_comp->data);
1611 l_tile_comp->data = 00;
1612 l_tile_comp->ownsData = 0;
1613 l_tile_comp->data_size = 0;
1614 l_tile_comp->data_size_needed = 0;
1619 opj_free(l_tile->comps);
1621 opj_free(p_tcd->tcd_image->tiles);
1622 p_tcd->tcd_image->tiles = 00;
1626 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1627 OPJ_BYTE * p_src_data,
1628 OPJ_UINT32 * p_data_read,
1629 OPJ_UINT32 p_max_src_size,
1630 opj_codestream_index_t *p_cstr_index,
1631 opj_event_mgr_t *p_manager
1636 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1641 if (! opj_t2_decode_packets(
1644 p_tcd->tcd_image->tiles,
1650 opj_t2_destroy(l_t2);
1654 opj_t2_destroy(l_t2);
1656 /*---------------CLEAN-------------------*/
1660 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd)
1663 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1664 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1665 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1666 volatile OPJ_BOOL ret = OPJ_TRUE;
1668 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1669 opj_t1_decode_cblks(p_tcd->thread_pool, &ret, l_tile_comp, l_tccp);
1677 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1683 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1686 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1687 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1688 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1689 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1691 for (compno = 0; compno < l_tile->numcomps; compno++) {
1693 if (tcd->cp->reduce != 0) {
1694 tcd->image->comps[compno].resno_decoded =
1695 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1696 if (tcd->image->comps[compno].resno_decoded < 0)
1701 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1702 if(numres2decode > 0){
1705 if (l_tccp->qmfbid == 1) {
1706 if (! opj_dwt_decode(p_tcd->thread_pool, l_tile_comp,
1707 l_img_comp->resno_decoded + 1)) {
1711 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded + 1)) {
1723 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1725 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1726 opj_tcp_t * l_tcp = p_tcd->tcp;
1727 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1728 OPJ_UINT32 l_samples, i;
1734 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1735 (l_tile_comp->y1 - l_tile_comp->y0));
1737 if (l_tile->numcomps >= 3) {
1738 /* testcase 1336.pdf.asan.47.376 */
1739 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 -
1740 l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1741 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 -
1742 l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1743 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 -
1744 l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1745 opj_event_msg(p_manager, EVT_ERROR,
1746 "Tiles don't all have the same dimension. Skip the MCT step.\n");
1748 } else if (l_tcp->mct == 2) {
1751 if (! l_tcp->m_mct_decoding_matrix) {
1755 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
1760 for (i = 0; i < l_tile->numcomps; ++i) {
1761 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1765 if (! opj_mct_decode_custom(/* MCT data */
1766 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1767 /* size of components */
1771 /* nb of components (i.e. size of pData) */
1773 /* tells if the data is signed */
1774 p_tcd->image->comps->sgnd)) {
1781 if (l_tcp->tccps->qmfbid == 1) {
1782 opj_mct_decode(l_tile->comps[0].data,
1783 l_tile->comps[1].data,
1784 l_tile->comps[2].data,
1787 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1788 (OPJ_FLOAT32*)l_tile->comps[1].data,
1789 (OPJ_FLOAT32*)l_tile->comps[2].data,
1794 opj_event_msg(p_manager, EVT_ERROR,
1795 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
1803 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
1806 opj_tcd_tilecomp_t * l_tile_comp = 00;
1807 opj_tccp_t * l_tccp = 00;
1808 opj_image_comp_t * l_img_comp = 00;
1809 opj_tcd_resolution_t* l_res = 00;
1810 opj_tcd_tile_t * l_tile;
1811 OPJ_UINT32 l_width, l_height, i, j;
1812 OPJ_INT32 * l_current_ptr;
1813 OPJ_INT32 l_min, l_max;
1814 OPJ_UINT32 l_stride;
1816 l_tile = p_tcd->tcd_image->tiles;
1817 l_tile_comp = l_tile->comps;
1818 l_tccp = p_tcd->tcp->tccps;
1819 l_img_comp = p_tcd->image->comps;
1821 for (compno = 0; compno < l_tile->numcomps; compno++) {
1822 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1823 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1824 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1825 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1827 assert(l_height == 0 ||
1828 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1830 if (l_img_comp->sgnd) {
1831 l_min = -(1 << (l_img_comp->prec - 1));
1832 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1835 l_max = (1 << l_img_comp->prec) - 1;
1838 l_current_ptr = l_tile_comp->data;
1840 if (l_tccp->qmfbid == 1) {
1841 for (j = 0; j < l_height; ++j) {
1842 for (i = 0; i < l_width; ++i) {
1843 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
1847 l_current_ptr += l_stride;
1850 for (j = 0; j < l_height; ++j) {
1851 for (i = 0; i < l_width; ++i) {
1852 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1853 *l_current_ptr = opj_int_clamp((OPJ_INT32)opj_lrintf(l_value) +
1854 l_tccp->m_dc_level_shift, l_min, l_max); ;
1857 l_current_ptr += l_stride;
1872 * Deallocates the encoding data of the given precinct.
1874 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
1876 OPJ_UINT32 cblkno, l_nb_code_blocks;
1878 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1880 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1881 /*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);*/
1882 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1883 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 );*/
1886 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1887 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1889 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1891 if (l_code_block->data) {
1892 opj_free(l_code_block->data);
1893 l_code_block->data = 00;
1896 if (l_code_block->segs) {
1897 opj_free(l_code_block->segs);
1898 l_code_block->segs = 00;
1904 opj_free(p_precinct->cblks.dec);
1905 p_precinct->cblks.dec = 00;
1910 * Deallocates the encoding data of the given precinct.
1912 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
1914 OPJ_UINT32 cblkno, l_nb_code_blocks;
1916 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1918 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1920 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1921 if (l_code_block->data) {
1922 /* We refer to data - 1 since below we incremented it */
1923 /* in opj_tcd_code_block_enc_allocate_data() */
1924 opj_free(l_code_block->data - 1);
1925 l_code_block->data = 00;
1928 if (l_code_block->layers) {
1929 opj_free(l_code_block->layers);
1930 l_code_block->layers = 00;
1933 if (l_code_block->passes) {
1934 opj_free(l_code_block->passes);
1935 l_code_block->passes = 00;
1940 opj_free(p_precinct->cblks.enc);
1942 p_precinct->cblks.enc = 00;
1946 OPJ_UINT32 opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
1948 OPJ_UINT32 i, l_data_size = 0;
1949 opj_image_comp_t * l_img_comp = 00;
1950 opj_tcd_tilecomp_t * l_tilec = 00;
1951 OPJ_UINT32 l_size_comp, l_remaining;
1953 l_tilec = p_tcd->tcd_image->tiles->comps;
1954 l_img_comp = p_tcd->image->comps;
1955 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1956 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1957 l_remaining = l_img_comp->prec & 7; /* (%8) */
1963 if (l_size_comp == 3) {
1967 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) *
1968 (l_tilec->y1 - l_tilec->y0));
1976 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
1979 opj_tcd_tilecomp_t * l_tile_comp = 00;
1980 opj_tccp_t * l_tccp = 00;
1981 opj_image_comp_t * l_img_comp = 00;
1982 opj_tcd_tile_t * l_tile;
1983 OPJ_UINT32 l_nb_elem, i;
1984 OPJ_INT32 * l_current_ptr;
1986 l_tile = p_tcd->tcd_image->tiles;
1987 l_tile_comp = l_tile->comps;
1988 l_tccp = p_tcd->tcp->tccps;
1989 l_img_comp = p_tcd->image->comps;
1991 for (compno = 0; compno < l_tile->numcomps; compno++) {
1992 l_current_ptr = l_tile_comp->data;
1993 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1994 (l_tile_comp->y1 - l_tile_comp->y0));
1996 if (l_tccp->qmfbid == 1) {
1997 for (i = 0; i < l_nb_elem; ++i) {
1998 *l_current_ptr -= l_tccp->m_dc_level_shift ;
2002 for (i = 0; i < l_nb_elem; ++i) {
2003 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2016 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2018 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2019 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2020 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2021 (l_tile_comp->y1 - l_tile_comp->y0));
2023 OPJ_BYTE ** l_data = 00;
2024 opj_tcp_t * l_tcp = p_tcd->tcp;
2026 if (!p_tcd->tcp->mct) {
2030 if (p_tcd->tcp->mct == 2) {
2031 if (! p_tcd->tcp->m_mct_coding_matrix) {
2035 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2040 for (i = 0; i < l_tile->numcomps; ++i) {
2041 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2045 if (! opj_mct_encode_custom(/* MCT data */
2046 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2047 /* size of components */
2051 /* nb of components (i.e. size of pData) */
2053 /* tells if the data is signed */
2054 p_tcd->image->comps->sgnd)) {
2060 } else if (l_tcp->tccps->qmfbid == 0) {
2061 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2062 l_tile->comps[2].data, samples);
2064 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2065 l_tile->comps[2].data, samples);
2071 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2073 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2074 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2075 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2078 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2079 if (l_tccp->qmfbid == 1) {
2080 if (! opj_dwt_encode(l_tile_comp)) {
2083 } else if (l_tccp->qmfbid == 0) {
2084 if (! opj_dwt_encode_real(l_tile_comp)) {
2096 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2099 const OPJ_FLOAT64 * l_mct_norms;
2100 OPJ_UINT32 l_mct_numcomps = 0U;
2101 opj_tcp_t * l_tcp = p_tcd->tcp;
2103 l_t1 = opj_t1_create(OPJ_TRUE);
2108 if (l_tcp->mct == 1) {
2109 l_mct_numcomps = 3U;
2110 /* irreversible encoding */
2111 if (l_tcp->tccps->qmfbid == 0) {
2112 l_mct_norms = opj_mct_get_mct_norms_real();
2114 l_mct_norms = opj_mct_get_mct_norms();
2117 l_mct_numcomps = p_tcd->image->numcomps;
2118 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2121 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2123 opj_t1_destroy(l_t1);
2127 opj_t1_destroy(l_t1);
2132 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2133 OPJ_BYTE * p_dest_data,
2134 OPJ_UINT32 * p_data_written,
2135 OPJ_UINT32 p_max_dest_size,
2136 opj_codestream_info_t *p_cstr_info)
2140 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2145 if (! opj_t2_encode_packets(
2148 p_tcd->tcd_image->tiles,
2149 p_tcd->tcp->numlayers,
2158 opj_t2_destroy(l_t2);
2162 opj_t2_destroy(l_t2);
2164 /*---------------CLEAN-------------------*/
2169 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2170 OPJ_BYTE * p_dest_data,
2171 OPJ_UINT32 p_max_dest_size,
2172 opj_codestream_info_t *p_cstr_info)
2174 opj_cp_t * l_cp = p_tcd->cp;
2175 OPJ_UINT32 l_nb_written = 0;
2178 p_cstr_info->index_write = 0;
2181 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2182 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2184 /* Normal Rate/distortion allocation */
2185 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2190 /* Fixed layer allocation */
2191 opj_tcd_rateallocate_fixed(p_tcd);
2198 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2200 OPJ_UINT32 p_src_length)
2202 OPJ_UINT32 i, j, l_data_size = 0;
2203 opj_image_comp_t * l_img_comp = 00;
2204 opj_tcd_tilecomp_t * l_tilec = 00;
2205 OPJ_UINT32 l_size_comp, l_remaining;
2206 OPJ_UINT32 l_nb_elem;
2208 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2209 if (l_data_size != p_src_length) {
2213 l_tilec = p_tcd->tcd_image->tiles->comps;
2214 l_img_comp = p_tcd->image->comps;
2215 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2216 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2217 l_remaining = l_img_comp->prec & 7; /* (%8) */
2218 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 -
2225 if (l_size_comp == 3) {
2229 switch (l_size_comp) {
2231 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2232 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2234 if (l_img_comp->sgnd) {
2235 for (j = 0; j < l_nb_elem; ++j) {
2236 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2239 for (j = 0; j < l_nb_elem; ++j) {
2240 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2244 p_src = (OPJ_BYTE*) l_src_ptr;
2248 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2249 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2251 if (l_img_comp->sgnd) {
2252 for (j = 0; j < l_nb_elem; ++j) {
2253 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2256 for (j = 0; j < l_nb_elem; ++j) {
2257 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2261 p_src = (OPJ_BYTE*) l_src_ptr;
2265 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2266 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2268 for (j = 0; j < l_nb_elem; ++j) {
2269 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2272 p_src = (OPJ_BYTE*) l_src_ptr;