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 opj_free(p_code_block->data - 1); /* again, why -1 */
1167 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1168 if (! p_code_block->data) {
1169 p_code_block->data_size = 0U;
1172 p_code_block->data_size = l_data_size;
1174 p_code_block->data[0] = 0;
1175 p_code_block->data += 1; /*why +1 ?*/
1181 * Allocates memory for a decoding code block.
1183 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1186 if (! p_code_block->data) {
1188 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1189 if (! p_code_block->data) {
1192 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1193 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1195 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1196 sizeof(opj_tcd_seg_t));
1197 if (! p_code_block->segs) {
1200 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1202 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1203 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1206 OPJ_BYTE* l_data = p_code_block->data;
1207 OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1208 opj_tcd_seg_t * l_segs = p_code_block->segs;
1209 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1211 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1212 p_code_block->data = l_data;
1213 p_code_block->data_max_size = l_data_max_size;
1214 p_code_block->segs = l_segs;
1215 p_code_block->m_current_max_segs = l_current_max_segs;
1221 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd)
1224 OPJ_UINT32 l_data_size = 0;
1225 opj_image_comp_t * l_img_comp = 00;
1226 opj_tcd_tilecomp_t * l_tile_comp = 00;
1227 opj_tcd_resolution_t * l_res = 00;
1228 OPJ_UINT32 l_size_comp, l_remaining;
1230 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1231 l_img_comp = p_tcd->image->comps;
1233 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1234 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1235 l_remaining = l_img_comp->prec & 7; /* (%8) */
1241 if (l_size_comp == 3) {
1245 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1246 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) *
1247 (l_res->y1 - l_res->y0));
1255 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1256 OPJ_UINT32 p_tile_no,
1258 OPJ_UINT32 * p_data_written,
1259 OPJ_UINT32 p_max_length,
1260 opj_codestream_info_t *p_cstr_info)
1263 if (p_tcd->cur_tp_num == 0) {
1265 p_tcd->tcd_tileno = p_tile_no;
1266 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1268 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1270 OPJ_UINT32 l_num_packs = 0;
1272 opj_tcd_tilecomp_t *l_tilec_idx =
1273 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1274 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1276 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1277 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1279 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1280 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1282 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1283 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1284 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1286 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1287 size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs,
1288 sizeof(opj_packet_info_t));
1289 if (!p_cstr_info->tile[p_tile_no].packet) {
1290 /* FIXME event manager error callback */
1296 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1297 /*---------------TILE-------------------*/
1298 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1301 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1303 /* FIXME _ProfStart(PGROUP_MCT); */
1304 if (! opj_tcd_mct_encode(p_tcd)) {
1307 /* FIXME _ProfStop(PGROUP_MCT); */
1309 /* FIXME _ProfStart(PGROUP_DWT); */
1310 if (! opj_tcd_dwt_encode(p_tcd)) {
1313 /* FIXME _ProfStop(PGROUP_DWT); */
1315 /* FIXME _ProfStart(PGROUP_T1); */
1316 if (! opj_tcd_t1_encode(p_tcd)) {
1319 /* FIXME _ProfStop(PGROUP_T1); */
1321 /* FIXME _ProfStart(PGROUP_RATE); */
1322 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length, p_cstr_info)) {
1325 /* FIXME _ProfStop(PGROUP_RATE); */
1328 /*--------------TIER2------------------*/
1332 p_cstr_info->index_write = 1;
1334 /* FIXME _ProfStart(PGROUP_T2); */
1336 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1340 /* FIXME _ProfStop(PGROUP_T2); */
1342 /*---------------CLEAN-------------------*/
1347 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1349 OPJ_UINT32 p_max_length,
1350 OPJ_UINT32 p_tile_no,
1351 opj_codestream_index_t *p_cstr_index,
1352 opj_event_mgr_t *p_manager
1355 OPJ_UINT32 l_data_read;
1356 p_tcd->tcd_tileno = p_tile_no;
1357 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1359 #ifdef TODO_MSD /* FIXME */
1362 OPJ_UINT32 resno, compno, numprec = 0;
1363 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1364 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1365 opj_tccp_t *tccp = &tcp->tccps[compno];
1366 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1367 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1368 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1369 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1370 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1371 numprec += res_idx->pw * res_idx->ph;
1372 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1373 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1376 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1377 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1378 p_cstr_info->packno = 0;
1383 /*--------------TIER2------------------*/
1384 /* FIXME _ProfStart(PGROUP_T2); */
1386 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1390 /* FIXME _ProfStop(PGROUP_T2); */
1392 /*------------------TIER1-----------------*/
1394 /* FIXME _ProfStart(PGROUP_T1); */
1396 (! opj_tcd_t1_decode(p_tcd)) {
1399 /* FIXME _ProfStop(PGROUP_T1); */
1401 /*----------------DWT---------------------*/
1403 /* FIXME _ProfStart(PGROUP_DWT); */
1405 (! opj_tcd_dwt_decode(p_tcd)) {
1408 /* FIXME _ProfStop(PGROUP_DWT); */
1410 /*----------------MCT-------------------*/
1411 /* FIXME _ProfStart(PGROUP_MCT); */
1413 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1416 /* FIXME _ProfStop(PGROUP_MCT); */
1418 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1420 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1423 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1426 /*---------------TILE-------------------*/
1430 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1432 OPJ_UINT32 p_dest_length
1435 OPJ_UINT32 i, j, k, l_data_size = 0;
1436 opj_image_comp_t * l_img_comp = 00;
1437 opj_tcd_tilecomp_t * l_tilec = 00;
1438 opj_tcd_resolution_t * l_res;
1439 OPJ_UINT32 l_size_comp, l_remaining;
1440 OPJ_UINT32 l_stride, l_width, l_height;
1442 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1443 if (l_data_size > p_dest_length) {
1447 l_tilec = p_tcd->tcd_image->tiles->comps;
1448 l_img_comp = p_tcd->image->comps;
1450 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1451 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1452 l_remaining = l_img_comp->prec & 7; /* (%8) */
1453 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1454 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1455 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1456 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1462 if (l_size_comp == 3) {
1466 switch (l_size_comp) {
1468 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1469 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1471 if (l_img_comp->sgnd) {
1472 for (j = 0; j < l_height; ++j) {
1473 for (k = 0; k < l_width; ++k) {
1474 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1476 l_src_ptr += l_stride;
1479 for (j = 0; j < l_height; ++j) {
1480 for (k = 0; k < l_width; ++k) {
1481 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1483 l_src_ptr += l_stride;
1487 p_dest = (OPJ_BYTE *)l_dest_ptr;
1491 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1492 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1494 if (l_img_comp->sgnd) {
1495 for (j = 0; j < l_height; ++j) {
1496 for (k = 0; k < l_width; ++k) {
1497 *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++));
1499 l_src_ptr += l_stride;
1502 for (j = 0; j < l_height; ++j) {
1503 for (k = 0; k < l_width; ++k) {
1504 *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1506 l_src_ptr += l_stride;
1510 p_dest = (OPJ_BYTE*) l_dest_ptr;
1514 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1515 OPJ_INT32 * l_src_ptr = l_tilec->data;
1517 for (j = 0; j < l_height; ++j) {
1518 for (k = 0; k < l_width; ++k) {
1519 *(l_dest_ptr++) = (*(l_src_ptr++));
1521 l_src_ptr += l_stride;
1524 p_dest = (OPJ_BYTE*) l_dest_ptr;
1539 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1541 OPJ_UINT32 compno, resno, bandno, precno;
1542 opj_tcd_tile_t *l_tile = 00;
1543 opj_tcd_tilecomp_t *l_tile_comp = 00;
1544 opj_tcd_resolution_t *l_res = 00;
1545 opj_tcd_band_t *l_band = 00;
1546 opj_tcd_precinct_t *l_precinct = 00;
1547 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1548 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1554 if (! p_tcd->tcd_image) {
1558 if (p_tcd->m_is_decoder) {
1559 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1561 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1564 l_tile = p_tcd->tcd_image->tiles;
1569 l_tile_comp = l_tile->comps;
1571 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1572 l_res = l_tile_comp->resolutions;
1575 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1576 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1577 l_band = l_res->bands;
1578 for (bandno = 0; bandno < 3; ++bandno) {
1579 l_precinct = l_band->precincts;
1582 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1583 for (precno = 0; precno < l_nb_precincts; ++precno) {
1584 opj_tgt_destroy(l_precinct->incltree);
1585 l_precinct->incltree = 00;
1586 opj_tgt_destroy(l_precinct->imsbtree);
1587 l_precinct->imsbtree = 00;
1588 (*l_tcd_code_block_deallocate)(l_precinct);
1592 opj_free(l_band->precincts);
1593 l_band->precincts = 00;
1600 opj_free(l_tile_comp->resolutions);
1601 l_tile_comp->resolutions = 00;
1604 if (l_tile_comp->ownsData && l_tile_comp->data) {
1605 opj_aligned_free(l_tile_comp->data);
1606 l_tile_comp->data = 00;
1607 l_tile_comp->ownsData = 0;
1608 l_tile_comp->data_size = 0;
1609 l_tile_comp->data_size_needed = 0;
1614 opj_free(l_tile->comps);
1616 opj_free(p_tcd->tcd_image->tiles);
1617 p_tcd->tcd_image->tiles = 00;
1621 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1622 OPJ_BYTE * p_src_data,
1623 OPJ_UINT32 * p_data_read,
1624 OPJ_UINT32 p_max_src_size,
1625 opj_codestream_index_t *p_cstr_index,
1626 opj_event_mgr_t *p_manager
1631 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1636 if (! opj_t2_decode_packets(
1639 p_tcd->tcd_image->tiles,
1645 opj_t2_destroy(l_t2);
1649 opj_t2_destroy(l_t2);
1651 /*---------------CLEAN-------------------*/
1655 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd)
1658 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1659 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1660 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1661 volatile OPJ_BOOL ret = OPJ_TRUE;
1663 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1664 opj_t1_decode_cblks(p_tcd->thread_pool, &ret, l_tile_comp, l_tccp);
1672 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1678 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1681 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1682 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1683 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1684 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1686 for (compno = 0; compno < l_tile->numcomps; compno++) {
1688 if (tcd->cp->reduce != 0) {
1689 tcd->image->comps[compno].resno_decoded =
1690 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1691 if (tcd->image->comps[compno].resno_decoded < 0)
1696 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1697 if(numres2decode > 0){
1700 if (l_tccp->qmfbid == 1) {
1701 if (! opj_dwt_decode(p_tcd->thread_pool, l_tile_comp,
1702 l_img_comp->resno_decoded + 1)) {
1706 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded + 1)) {
1718 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1720 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1721 opj_tcp_t * l_tcp = p_tcd->tcp;
1722 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1723 OPJ_UINT32 l_samples, i;
1729 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1730 (l_tile_comp->y1 - l_tile_comp->y0));
1732 if (l_tile->numcomps >= 3) {
1733 /* testcase 1336.pdf.asan.47.376 */
1734 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 -
1735 l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1736 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 -
1737 l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1738 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 -
1739 l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1740 opj_event_msg(p_manager, EVT_ERROR,
1741 "Tiles don't all have the same dimension. Skip the MCT step.\n");
1743 } else if (l_tcp->mct == 2) {
1746 if (! l_tcp->m_mct_decoding_matrix) {
1750 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
1755 for (i = 0; i < l_tile->numcomps; ++i) {
1756 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1760 if (! opj_mct_decode_custom(/* MCT data */
1761 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1762 /* size of components */
1766 /* nb of components (i.e. size of pData) */
1768 /* tells if the data is signed */
1769 p_tcd->image->comps->sgnd)) {
1776 if (l_tcp->tccps->qmfbid == 1) {
1777 opj_mct_decode(l_tile->comps[0].data,
1778 l_tile->comps[1].data,
1779 l_tile->comps[2].data,
1782 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1783 (OPJ_FLOAT32*)l_tile->comps[1].data,
1784 (OPJ_FLOAT32*)l_tile->comps[2].data,
1789 opj_event_msg(p_manager, EVT_ERROR,
1790 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
1798 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
1801 opj_tcd_tilecomp_t * l_tile_comp = 00;
1802 opj_tccp_t * l_tccp = 00;
1803 opj_image_comp_t * l_img_comp = 00;
1804 opj_tcd_resolution_t* l_res = 00;
1805 opj_tcd_tile_t * l_tile;
1806 OPJ_UINT32 l_width, l_height, i, j;
1807 OPJ_INT32 * l_current_ptr;
1808 OPJ_INT32 l_min, l_max;
1809 OPJ_UINT32 l_stride;
1811 l_tile = p_tcd->tcd_image->tiles;
1812 l_tile_comp = l_tile->comps;
1813 l_tccp = p_tcd->tcp->tccps;
1814 l_img_comp = p_tcd->image->comps;
1816 for (compno = 0; compno < l_tile->numcomps; compno++) {
1817 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1818 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1819 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1820 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1822 assert(l_height == 0 ||
1823 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1825 if (l_img_comp->sgnd) {
1826 l_min = -(1 << (l_img_comp->prec - 1));
1827 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1830 l_max = (1 << l_img_comp->prec) - 1;
1833 l_current_ptr = l_tile_comp->data;
1835 if (l_tccp->qmfbid == 1) {
1836 for (j = 0; j < l_height; ++j) {
1837 for (i = 0; i < l_width; ++i) {
1838 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
1842 l_current_ptr += l_stride;
1845 for (j = 0; j < l_height; ++j) {
1846 for (i = 0; i < l_width; ++i) {
1847 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1848 *l_current_ptr = opj_int_clamp((OPJ_INT32)opj_lrintf(l_value) +
1849 l_tccp->m_dc_level_shift, l_min, l_max); ;
1852 l_current_ptr += l_stride;
1867 * Deallocates the encoding data of the given precinct.
1869 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
1871 OPJ_UINT32 cblkno, l_nb_code_blocks;
1873 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1875 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1876 /*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);*/
1877 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1878 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 );*/
1881 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1882 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1884 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1886 if (l_code_block->data) {
1887 opj_free(l_code_block->data);
1888 l_code_block->data = 00;
1891 if (l_code_block->segs) {
1892 opj_free(l_code_block->segs);
1893 l_code_block->segs = 00;
1899 opj_free(p_precinct->cblks.dec);
1900 p_precinct->cblks.dec = 00;
1905 * Deallocates the encoding data of the given precinct.
1907 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
1909 OPJ_UINT32 cblkno, l_nb_code_blocks;
1911 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1913 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1915 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1916 if (l_code_block->data) {
1917 opj_free(l_code_block->data - 1);
1918 l_code_block->data = 00;
1921 if (l_code_block->layers) {
1922 opj_free(l_code_block->layers);
1923 l_code_block->layers = 00;
1926 if (l_code_block->passes) {
1927 opj_free(l_code_block->passes);
1928 l_code_block->passes = 00;
1933 opj_free(p_precinct->cblks.enc);
1935 p_precinct->cblks.enc = 00;
1939 OPJ_UINT32 opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
1941 OPJ_UINT32 i, l_data_size = 0;
1942 opj_image_comp_t * l_img_comp = 00;
1943 opj_tcd_tilecomp_t * l_tilec = 00;
1944 OPJ_UINT32 l_size_comp, l_remaining;
1946 l_tilec = p_tcd->tcd_image->tiles->comps;
1947 l_img_comp = p_tcd->image->comps;
1948 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1949 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1950 l_remaining = l_img_comp->prec & 7; /* (%8) */
1956 if (l_size_comp == 3) {
1960 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) *
1961 (l_tilec->y1 - l_tilec->y0));
1969 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
1972 opj_tcd_tilecomp_t * l_tile_comp = 00;
1973 opj_tccp_t * l_tccp = 00;
1974 opj_image_comp_t * l_img_comp = 00;
1975 opj_tcd_tile_t * l_tile;
1976 OPJ_UINT32 l_nb_elem, i;
1977 OPJ_INT32 * l_current_ptr;
1979 l_tile = p_tcd->tcd_image->tiles;
1980 l_tile_comp = l_tile->comps;
1981 l_tccp = p_tcd->tcp->tccps;
1982 l_img_comp = p_tcd->image->comps;
1984 for (compno = 0; compno < l_tile->numcomps; compno++) {
1985 l_current_ptr = l_tile_comp->data;
1986 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1987 (l_tile_comp->y1 - l_tile_comp->y0));
1989 if (l_tccp->qmfbid == 1) {
1990 for (i = 0; i < l_nb_elem; ++i) {
1991 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1995 for (i = 0; i < l_nb_elem; ++i) {
1996 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2009 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2011 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2012 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2013 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2014 (l_tile_comp->y1 - l_tile_comp->y0));
2016 OPJ_BYTE ** l_data = 00;
2017 opj_tcp_t * l_tcp = p_tcd->tcp;
2019 if (!p_tcd->tcp->mct) {
2023 if (p_tcd->tcp->mct == 2) {
2024 if (! p_tcd->tcp->m_mct_coding_matrix) {
2028 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2033 for (i = 0; i < l_tile->numcomps; ++i) {
2034 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2038 if (! opj_mct_encode_custom(/* MCT data */
2039 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2040 /* size of components */
2044 /* nb of components (i.e. size of pData) */
2046 /* tells if the data is signed */
2047 p_tcd->image->comps->sgnd)) {
2053 } else if (l_tcp->tccps->qmfbid == 0) {
2054 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2055 l_tile->comps[2].data, samples);
2057 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2058 l_tile->comps[2].data, samples);
2064 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2066 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2067 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2068 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2071 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2072 if (l_tccp->qmfbid == 1) {
2073 if (! opj_dwt_encode(l_tile_comp)) {
2076 } else if (l_tccp->qmfbid == 0) {
2077 if (! opj_dwt_encode_real(l_tile_comp)) {
2089 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2092 const OPJ_FLOAT64 * l_mct_norms;
2093 OPJ_UINT32 l_mct_numcomps = 0U;
2094 opj_tcp_t * l_tcp = p_tcd->tcp;
2096 l_t1 = opj_t1_create(OPJ_TRUE);
2101 if (l_tcp->mct == 1) {
2102 l_mct_numcomps = 3U;
2103 /* irreversible encoding */
2104 if (l_tcp->tccps->qmfbid == 0) {
2105 l_mct_norms = opj_mct_get_mct_norms_real();
2107 l_mct_norms = opj_mct_get_mct_norms();
2110 l_mct_numcomps = p_tcd->image->numcomps;
2111 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2114 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2116 opj_t1_destroy(l_t1);
2120 opj_t1_destroy(l_t1);
2125 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2126 OPJ_BYTE * p_dest_data,
2127 OPJ_UINT32 * p_data_written,
2128 OPJ_UINT32 p_max_dest_size,
2129 opj_codestream_info_t *p_cstr_info)
2133 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2138 if (! opj_t2_encode_packets(
2141 p_tcd->tcd_image->tiles,
2142 p_tcd->tcp->numlayers,
2151 opj_t2_destroy(l_t2);
2155 opj_t2_destroy(l_t2);
2157 /*---------------CLEAN-------------------*/
2162 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2163 OPJ_BYTE * p_dest_data,
2164 OPJ_UINT32 p_max_dest_size,
2165 opj_codestream_info_t *p_cstr_info)
2167 opj_cp_t * l_cp = p_tcd->cp;
2168 OPJ_UINT32 l_nb_written = 0;
2171 p_cstr_info->index_write = 0;
2174 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2175 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2177 /* Normal Rate/distortion allocation */
2178 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2183 /* Fixed layer allocation */
2184 opj_tcd_rateallocate_fixed(p_tcd);
2191 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2193 OPJ_UINT32 p_src_length)
2195 OPJ_UINT32 i, j, l_data_size = 0;
2196 opj_image_comp_t * l_img_comp = 00;
2197 opj_tcd_tilecomp_t * l_tilec = 00;
2198 OPJ_UINT32 l_size_comp, l_remaining;
2199 OPJ_UINT32 l_nb_elem;
2201 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2202 if (l_data_size != p_src_length) {
2206 l_tilec = p_tcd->tcd_image->tiles->comps;
2207 l_img_comp = p_tcd->image->comps;
2208 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2209 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2210 l_remaining = l_img_comp->prec & 7; /* (%8) */
2211 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 -
2218 if (l_size_comp == 3) {
2222 switch (l_size_comp) {
2224 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2225 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2227 if (l_img_comp->sgnd) {
2228 for (j = 0; j < l_nb_elem; ++j) {
2229 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2232 for (j = 0; j < l_nb_elem; ++j) {
2233 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2237 p_src = (OPJ_BYTE*) l_src_ptr;
2241 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2242 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2244 if (l_img_comp->sgnd) {
2245 for (j = 0; j < l_nb_elem; ++j) {
2246 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2249 for (j = 0; j < l_nb_elem; ++j) {
2250 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2254 p_src = (OPJ_BYTE*) l_src_ptr;
2258 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2259 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2261 for (j = 0; j < l_nb_elem; ++j) {
2262 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2265 p_src = (OPJ_BYTE*) l_src_ptr;