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) {
48 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
50 fprintf(fd, "image {\n");
51 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
52 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
54 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
55 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
56 fprintf(fd, " tile {\n");
57 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
58 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
59 for (compno = 0; compno < tile->numcomps; compno++) {
60 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
61 fprintf(fd, " tilec {\n");
63 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
64 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
65 for (resno = 0; resno < tilec->numresolutions; resno++) {
66 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
67 fprintf(fd, "\n res {\n");
69 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
70 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
71 for (bandno = 0; bandno < res->numbands; bandno++) {
72 opj_tcd_band_t *band = &res->bands[bandno];
73 fprintf(fd, " band {\n");
75 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
76 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
77 for (precno = 0; precno < res->pw * res->ph; precno++) {
78 opj_tcd_precinct_t *prec = &band->precincts[precno];
79 fprintf(fd, " prec {\n");
81 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
82 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
84 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
85 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
86 fprintf(fd, " cblk {\n");
88 " x0=%d, y0=%d, x1=%d, y1=%d\n",
89 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
108 * Initializes tile coding/decoding
110 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block, opj_event_mgr_t* manager);
113 * Allocates memory for a decoding code block.
115 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
118 * Deallocates the decoding data of the given precinct.
120 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
123 * Allocates memory for an encoding code block (but not data).
125 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
128 * Allocates data for an encoding code block
130 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block);
133 * Deallocates the encoding data of the given precinct.
135 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
139 Free the memory allocated for encoding
140 @param tcd TCD handle
142 static void opj_tcd_free_tile(opj_tcd_t *tcd);
145 static OPJ_BOOL opj_tcd_t2_decode ( opj_tcd_t *p_tcd,
146 OPJ_BYTE * p_src_data,
147 OPJ_UINT32 * p_data_read,
148 OPJ_UINT32 p_max_src_size,
149 opj_codestream_index_t *p_cstr_index,
150 opj_event_mgr_t *p_manager);
152 static OPJ_BOOL opj_tcd_t1_decode (opj_tcd_t *p_tcd);
154 static OPJ_BOOL opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
156 static OPJ_BOOL opj_tcd_mct_decode (opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager);
158 static OPJ_BOOL opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
161 static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
163 static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
165 static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
167 static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
169 static OPJ_BOOL opj_tcd_t2_encode ( opj_tcd_t *p_tcd,
170 OPJ_BYTE * p_dest_data,
171 OPJ_UINT32 * p_data_written,
172 OPJ_UINT32 p_max_dest_size,
173 opj_codestream_info_t *p_cstr_info );
175 static OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
176 OPJ_BYTE * p_dest_data,
177 OPJ_UINT32 p_max_dest_size,
178 opj_codestream_info_t *p_cstr_info );
180 /* ----------------------------------------------------------------------- */
183 Create a new TCD handle
185 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
187 opj_tcd_t *l_tcd = 00;
189 /* create the tcd structure */
190 l_tcd = (opj_tcd_t*) opj_calloc(1,sizeof(opj_tcd_t));
195 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
197 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1,sizeof(opj_tcd_image_t));
198 if (!l_tcd->tcd_image) {
207 /* ----------------------------------------------------------------------- */
209 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
212 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
213 opj_tcd_makelayer_fixed(tcd, layno, 1);
218 void opj_tcd_makelayer( opj_tcd_t *tcd,
223 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
226 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
228 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
230 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
231 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
233 for (resno = 0; resno < tilec->numresolutions; resno++) {
234 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
236 for (bandno = 0; bandno < res->numbands; bandno++) {
237 opj_tcd_band_t *band = &res->bands[bandno];
239 for (precno = 0; precno < res->pw * res->ph; precno++) {
240 opj_tcd_precinct_t *prc = &band->precincts[precno];
242 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
243 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
244 opj_tcd_layer_t *layer = &cblk->layers[layno];
248 cblk->numpassesinlayers = 0;
251 n = cblk->numpassesinlayers;
253 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
256 opj_tcd_pass_t *pass = &cblk->passes[passno];
260 dd = pass->distortiondec;
262 dr = pass->rate - cblk->passes[n - 1].rate;
263 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
271 if (thresh - (dd / dr) < DBL_EPSILON) /* do not rely on float equality, check with DBL_EPSILON margin */
275 layer->numpasses = n - cblk->numpassesinlayers;
277 if (!layer->numpasses) {
282 if (cblk->numpassesinlayers == 0) {
283 layer->len = cblk->passes[n - 1].rate;
284 layer->data = cblk->data;
285 layer->disto = cblk->passes[n - 1].distortiondec;
287 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
288 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
289 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
292 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
295 cblk->numpassesinlayers = n;
303 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
304 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
305 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
306 OPJ_INT32 matrice[10][10][3];
309 opj_cp_t *cp = tcd->cp;
310 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
311 opj_tcp_t *tcd_tcp = tcd->tcp;
313 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
314 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
316 for (i = 0; i < tcd_tcp->numlayers; i++) {
317 for (j = 0; j < tilec->numresolutions; j++) {
318 for (k = 0; k < 3; k++) {
320 (OPJ_INT32) ((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
321 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
326 for (resno = 0; resno < tilec->numresolutions; resno++) {
327 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
329 for (bandno = 0; bandno < res->numbands; bandno++) {
330 opj_tcd_band_t *band = &res->bands[bandno];
332 for (precno = 0; precno < res->pw * res->ph; precno++) {
333 opj_tcd_precinct_t *prc = &band->precincts[precno];
335 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
336 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
337 opj_tcd_layer_t *layer = &cblk->layers[layno];
339 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
341 /* Correction of the matrix of coefficient to include the IMSB information */
343 value = matrice[layno][resno][bandno];
350 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
351 if (imsb >= matrice[layno - 1][resno][bandno]) {
352 value -= (imsb - matrice[layno - 1][resno][bandno]);
360 cblk->numpassesinlayers = 0;
363 n = cblk->numpassesinlayers;
364 if (cblk->numpassesinlayers == 0) {
366 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
368 n = cblk->numpassesinlayers;
371 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
374 layer->numpasses = n - cblk->numpassesinlayers;
376 if (!layer->numpasses)
379 if (cblk->numpassesinlayers == 0) {
380 layer->len = cblk->passes[n - 1].rate;
381 layer->data = cblk->data;
383 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
384 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
388 cblk->numpassesinlayers = n;
396 OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
398 OPJ_UINT32 * p_data_written,
400 opj_codestream_info_t *cstr_info)
402 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
404 OPJ_FLOAT64 min, max;
405 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
406 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
407 OPJ_FLOAT64 maxSE = 0;
409 opj_cp_t *cp = tcd->cp;
410 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
411 opj_tcp_t *tcd_tcp = tcd->tcp;
416 tcd_tile->numpix = 0; /* fixed_quality */
418 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
419 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
422 for (resno = 0; resno < tilec->numresolutions; resno++) {
423 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
425 for (bandno = 0; bandno < res->numbands; bandno++) {
426 opj_tcd_band_t *band = &res->bands[bandno];
428 for (precno = 0; precno < res->pw * res->ph; precno++) {
429 opj_tcd_precinct_t *prc = &band->precincts[precno];
431 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
432 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
434 for (passno = 0; passno < cblk->totalpasses; passno++) {
435 opj_tcd_pass_t *pass = &cblk->passes[passno];
437 OPJ_FLOAT64 dd, rdslope;
440 dr = (OPJ_INT32)pass->rate;
441 dd = pass->distortiondec;
443 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
444 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
462 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
463 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
469 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
470 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
471 * ((OPJ_FLOAT64)(tilec->numpix));
476 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
477 tile_info->numpix = tcd_tile->numpix;
478 tile_info->distotile = tcd_tile->distotile;
479 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
480 if (!tile_info->thresh) {
481 /* FIXME event manager error callback */
486 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
487 OPJ_FLOAT64 lo = min;
488 OPJ_FLOAT64 hi = max;
489 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
490 OPJ_FLOAT64 goodthresh = 0;
491 OPJ_FLOAT64 stable_thresh = 0;
493 OPJ_FLOAT64 distotarget; /* fixed_quality */
496 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
498 /* Don't try to find an optimal threshold but rather take everything not included yet, if
499 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
500 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
501 ==> possible to have some lossy layers and the last layer for sure lossless */
502 if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0.0f)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0.0))) {
503 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
504 OPJ_FLOAT64 thresh = 0;
510 for (i = 0; i < 128; ++i) {
511 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
513 thresh = (lo + hi) / 2;
515 opj_tcd_makelayer(tcd, layno, thresh, 0);
517 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
518 if(OPJ_IS_CINEMA(cp->rsiz)){
519 if (! opj_t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) {
525 distoachieved = layno == 0 ?
526 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
528 if (distoachieved < distotarget) {
530 stable_thresh = thresh;
537 distoachieved = (layno == 0) ?
538 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
540 if (distoachieved < distotarget) {
542 stable_thresh = thresh;
548 if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC))
550 /* TODO: what to do with l ??? seek / tell ??? */
551 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
557 stable_thresh = thresh;
561 goodthresh = stable_thresh == 0? thresh : stable_thresh;
568 if(cstr_info) { /* Threshold for Marcela Index */
569 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
572 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
575 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
581 OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
582 opj_image_t * p_image,
584 opj_thread_pool_t* p_tp )
586 p_tcd->image = p_image;
589 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,sizeof(opj_tcd_tile_t));
590 if (! p_tcd->tcd_image->tiles) {
594 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(p_image->numcomps,sizeof(opj_tcd_tilecomp_t));
595 if (! p_tcd->tcd_image->tiles->comps ) {
599 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
600 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
601 p_tcd->thread_pool = p_tp;
607 Destroy a previously created TCD handle
609 void opj_tcd_destroy(opj_tcd_t *tcd) {
611 opj_tcd_free_tile(tcd);
613 if (tcd->tcd_image) {
614 opj_free(tcd->tcd_image);
621 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
623 if ((l_tilec->data == 00) || ((l_tilec->data_size_needed > l_tilec->data_size) && (l_tilec->ownsData == OPJ_FALSE))) {
624 l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed);
625 if (! l_tilec->data ) {
628 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
629 l_tilec->data_size = l_tilec->data_size_needed;
630 l_tilec->ownsData = OPJ_TRUE;
632 else if (l_tilec->data_size_needed > l_tilec->data_size) {
633 /* We don't need to keep old data */
634 opj_aligned_free(l_tilec->data);
635 l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed);
636 if (! l_tilec->data ) {
637 l_tilec->data_size = 0;
638 l_tilec->data_size_needed = 0;
639 l_tilec->ownsData = OPJ_FALSE;
642 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
643 l_tilec->data_size = l_tilec->data_size_needed;
644 l_tilec->ownsData = OPJ_TRUE;
649 /* ----------------------------------------------------------------------- */
651 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block, opj_event_mgr_t* manager)
653 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00;
654 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
655 opj_tcp_t * l_tcp = 00;
656 opj_cp_t * l_cp = 00;
657 opj_tcd_tile_t * l_tile = 00;
658 opj_tccp_t *l_tccp = 00;
659 opj_tcd_tilecomp_t *l_tilec = 00;
660 opj_image_comp_t * l_image_comp = 00;
661 opj_tcd_resolution_t *l_res = 00;
662 opj_tcd_band_t *l_band = 00;
663 opj_stepsize_t * l_step_size = 00;
664 opj_tcd_precinct_t *l_current_precinct = 00;
665 opj_image_t *l_image = 00;
667 OPJ_UINT32 l_level_no;
668 OPJ_UINT32 l_pdx, l_pdy;
670 OPJ_INT32 l_x0b, l_y0b;
671 OPJ_UINT32 l_tx0, l_ty0;
672 /* extent of precincts , top left, bottom right**/
673 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
674 /* number of precinct for a resolution */
675 OPJ_UINT32 l_nb_precincts;
676 /* room needed to store l_nb_precinct precinct for a resolution */
677 OPJ_UINT32 l_nb_precinct_size;
678 /* number of code blocks for a precinct*/
679 OPJ_UINT32 l_nb_code_blocks;
680 /* room needed to store l_nb_code_blocks code blocks for a precinct*/
681 OPJ_UINT32 l_nb_code_blocks_size;
682 /* size of data for a tile */
683 OPJ_UINT32 l_data_size;
686 l_tcp = &(l_cp->tcps[p_tile_no]);
687 l_tile = p_tcd->tcd_image->tiles;
688 l_tccp = l_tcp->tccps;
689 l_tilec = l_tile->comps;
690 l_image = p_tcd->image;
691 l_image_comp = p_tcd->image->comps;
693 p = p_tile_no % l_cp->tw; /* tile coordinates */
694 q = p_tile_no / l_cp->tw;
695 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
697 /* 4 borders of the tile rescale on the image if necessary */
698 l_tx0 = l_cp->tx0 + p * l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
699 l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
700 l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx), l_image->x1);
701 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
702 if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
703 opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
706 l_ty0 = l_cp->ty0 + q * l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
707 l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
708 l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy), l_image->y1);
709 /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
710 if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
711 opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
716 /* testcase 1888.pdf.asan.35.988 */
717 if (l_tccp->numresolutions == 0) {
718 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
721 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
723 /*tile->numcomps = image->numcomps; */
724 for (compno = 0; compno < l_tile->numcomps; ++compno) {
725 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
726 l_image_comp->resno_decoded = 0;
727 /* border of each l_tile component (global) */
728 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
729 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
730 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
731 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
732 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
734 /* compute l_data_size with overflow check */
735 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
736 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
737 if ((l_data_size > 0U) && ((((OPJ_UINT32)-1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0))) {
738 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
741 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
743 if ((((OPJ_UINT32)-1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
744 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
747 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
748 l_tilec->numresolutions = l_tccp->numresolutions;
749 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
750 l_tilec->minimum_num_resolutions = 1;
753 l_tilec->minimum_num_resolutions = l_tccp->numresolutions - l_cp->m_specific_param.m_dec.m_reduce;
756 l_tilec->data_size_needed = l_data_size;
757 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
758 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
762 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t);
764 if (l_tilec->resolutions == 00) {
765 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
766 if (! l_tilec->resolutions ) {
769 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
770 l_tilec->resolutions_size = l_data_size;
771 memset(l_tilec->resolutions,0,l_data_size);
773 else if (l_data_size > l_tilec->resolutions_size) {
774 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size);
775 if (! new_resolutions) {
776 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
777 opj_free(l_tilec->resolutions);
778 l_tilec->resolutions = NULL;
779 l_tilec->resolutions_size = 0;
782 l_tilec->resolutions = new_resolutions;
783 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
784 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size);
785 l_tilec->resolutions_size = l_data_size;
788 l_level_no = l_tilec->numresolutions;
789 l_res = l_tilec->resolutions;
790 l_step_size = l_tccp->stepsizes;
791 if (l_tccp->qmfbid == 0) {
792 l_gain_ptr = &opj_dwt_getgain_real;
795 l_gain_ptr = &opj_dwt_getgain;
797 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
799 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
800 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
801 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
802 OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
803 OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
807 /* border for each resolution level (global) */
808 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
809 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
810 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
811 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
812 /*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);*/
813 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
814 l_pdx = l_tccp->prcw[resno];
815 l_pdy = l_tccp->prch[resno];
816 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
817 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
818 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
819 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
820 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
821 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
822 /*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 );*/
824 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
825 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
826 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
828 if ((l_res->pw != 0U) && ((((OPJ_UINT32)-1) / l_res->pw) < l_res->ph)) {
829 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
832 l_nb_precincts = l_res->pw * l_res->ph;
834 if ((((OPJ_UINT32)-1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) < l_nb_precincts) {
835 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
838 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
841 tlcbgxstart = l_tl_prc_x_start;
842 tlcbgystart = l_tl_prc_y_start;
843 /*brcbgxend = l_br_prc_x_end;*/
844 /* brcbgyend = l_br_prc_y_end;*/
845 cbgwidthexpn = l_pdx;
846 cbgheightexpn = l_pdy;
850 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
851 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
852 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
853 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
854 cbgwidthexpn = l_pdx - 1;
855 cbgheightexpn = l_pdy - 1;
859 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
860 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
861 l_band = l_res->bands;
863 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
865 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
869 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
870 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
871 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
872 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
875 l_band->bandno = bandno + 1;
876 /* x0b = 1 if bandno = 1 or 3 */
877 l_x0b = l_band->bandno&1;
878 /* y0b = 1 if bandno = 2 or 3 */
879 l_y0b = (OPJ_INT32)((l_band->bandno)>>1);
880 /* l_band border (global) */
881 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b << l_level_no), (OPJ_INT32)(l_level_no + 1));
882 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b << l_level_no), (OPJ_INT32)(l_level_no + 1));
883 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b << l_level_no), (OPJ_INT32)(l_level_no + 1));
884 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b << l_level_no), (OPJ_INT32)(l_level_no + 1));
887 /** avoid an if with storing function pointer */
888 l_gain = (*l_gain_ptr) (l_band->bandno);
889 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
890 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * fraction;
891 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1; /* WHY -1 ? */
893 if (!l_band->precincts && (l_nb_precincts > 0U)) {
894 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size);
895 if (! l_band->precincts) {
896 opj_event_msg(manager, EVT_ERROR, "Not enough memory to handle band precints\n");
899 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
900 memset(l_band->precincts,0,l_nb_precinct_size);
901 l_band->precincts_data_size = l_nb_precinct_size;
903 else if (l_band->precincts_data_size < l_nb_precinct_size) {
905 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);
906 if (! new_precincts) {
907 opj_event_msg(manager, EVT_ERROR, "Not enough memory to handle band precints\n");
908 opj_free(l_band->precincts);
909 l_band->precincts = NULL;
910 l_band->precincts_data_size = 0;
913 l_band->precincts = new_precincts;
914 /*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);*/
915 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);
916 l_band->precincts_data_size = l_nb_precinct_size;
919 l_current_precinct = l_band->precincts;
920 for (precno = 0; precno < l_nb_precincts; ++precno) {
921 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
922 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn);
923 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn);
924 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
925 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
926 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
927 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
929 /* precinct size (global) */
930 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
932 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
933 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
934 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
935 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
936 /*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);*/
938 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
939 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
940 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
941 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
942 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
943 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
944 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
945 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
946 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn);
947 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn);
949 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
950 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
951 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
953 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
954 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
955 if (! l_current_precinct->cblks.blocks ) {
958 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
960 memset(l_current_precinct->cblks.blocks,0,l_nb_code_blocks_size);
962 l_current_precinct->block_size = l_nb_code_blocks_size;
964 else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
965 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks, l_nb_code_blocks_size);
967 opj_free(l_current_precinct->cblks.blocks);
968 l_current_precinct->cblks.blocks = NULL;
969 l_current_precinct->block_size = 0;
970 opj_event_msg(manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n");
973 l_current_precinct->cblks.blocks = new_blocks;
974 /*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); */
976 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) + l_current_precinct->block_size
978 ,l_nb_code_blocks_size - l_current_precinct->block_size);
980 l_current_precinct->block_size = l_nb_code_blocks_size;
983 if (! l_current_precinct->incltree) {
984 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, l_current_precinct->ch, manager);
987 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, l_current_precinct->cw, l_current_precinct->ch, manager);
990 if (! l_current_precinct->incltree) {
991 opj_event_msg(manager, EVT_WARNING, "No incltree created.\n");
992 /*return OPJ_FALSE;*/
995 if (! l_current_precinct->imsbtree) {
996 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw, l_current_precinct->ch, manager);
999 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree, l_current_precinct->cw, l_current_precinct->ch, manager);
1002 if (! l_current_precinct->imsbtree) {
1003 opj_event_msg(manager, EVT_WARNING, "No imsbtree created.\n");
1004 /*return OPJ_FALSE;*/
1007 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1008 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);
1009 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);
1010 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1011 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1014 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1016 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1019 /* code-block size (global) */
1020 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1021 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1022 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1023 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1025 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1029 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1031 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1034 /* code-block size (global) */
1035 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1036 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1037 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1038 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1041 ++l_current_precinct;
1055 OPJ_BOOL opj_tcd_init_encode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, opj_event_mgr_t* p_manager)
1057 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F, sizeof(opj_tcd_cblk_enc_t), p_manager);
1060 OPJ_BOOL opj_tcd_init_decode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, opj_event_mgr_t* p_manager)
1062 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F, sizeof(opj_tcd_cblk_dec_t), p_manager);
1066 * Allocates memory for an encoding code block (but not data memory).
1068 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1070 if (! p_code_block->layers) {
1071 /* no memset since data */
1072 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
1073 if (! p_code_block->layers) {
1077 if (! p_code_block->passes) {
1078 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
1079 if (! p_code_block->passes) {
1087 * Allocates data memory for an encoding code block.
1089 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block)
1091 OPJ_UINT32 l_data_size;
1093 l_data_size = (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) * (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1095 if (l_data_size > p_code_block->data_size) {
1096 if (p_code_block->data) {
1097 opj_free(p_code_block->data - 1); /* again, why -1 */
1099 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size+1);
1100 if(! p_code_block->data) {
1101 p_code_block->data_size = 0U;
1104 p_code_block->data_size = l_data_size;
1106 p_code_block->data[0] = 0;
1107 p_code_block->data+=1; /*why +1 ?*/
1113 * Allocates memory for a decoding code block.
1115 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1117 if (! p_code_block->data) {
1119 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1120 if (! p_code_block->data) {
1123 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1124 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1126 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,sizeof(opj_tcd_seg_t));
1127 if (! p_code_block->segs) {
1130 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1132 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1133 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1136 OPJ_BYTE* l_data = p_code_block->data;
1137 OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1138 opj_tcd_seg_t * l_segs = p_code_block->segs;
1139 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1141 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1142 p_code_block->data = l_data;
1143 p_code_block->data_max_size = l_data_max_size;
1144 p_code_block->segs = l_segs;
1145 p_code_block->m_current_max_segs = l_current_max_segs;
1151 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1154 OPJ_UINT32 l_data_size = 0;
1155 opj_image_comp_t * l_img_comp = 00;
1156 opj_tcd_tilecomp_t * l_tile_comp = 00;
1157 opj_tcd_resolution_t * l_res = 00;
1158 OPJ_UINT32 l_size_comp, l_remaining;
1160 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1161 l_img_comp = p_tcd->image->comps;
1163 for (i=0;i<p_tcd->image->numcomps;++i) {
1164 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1165 l_remaining = l_img_comp->prec & 7; /* (%8) */
1171 if (l_size_comp == 3) {
1175 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1176 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1184 OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd,
1185 OPJ_UINT32 p_tile_no,
1187 OPJ_UINT32 * p_data_written,
1188 OPJ_UINT32 p_max_length,
1189 opj_codestream_info_t *p_cstr_info)
1192 if (p_tcd->cur_tp_num == 0) {
1194 p_tcd->tcd_tileno = p_tile_no;
1195 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1197 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1199 OPJ_UINT32 l_num_packs = 0;
1201 opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1202 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1204 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1205 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1207 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1208 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1210 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1211 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1212 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1214 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t));
1215 if (!p_cstr_info->tile[p_tile_no].packet) {
1216 /* FIXME event manager error callback */
1222 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1223 /*---------------TILE-------------------*/
1224 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1227 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1229 /* FIXME _ProfStart(PGROUP_MCT); */
1230 if (! opj_tcd_mct_encode(p_tcd)) {
1233 /* FIXME _ProfStop(PGROUP_MCT); */
1235 /* FIXME _ProfStart(PGROUP_DWT); */
1236 if (! opj_tcd_dwt_encode(p_tcd)) {
1239 /* FIXME _ProfStop(PGROUP_DWT); */
1241 /* FIXME _ProfStart(PGROUP_T1); */
1242 if (! opj_tcd_t1_encode(p_tcd)) {
1245 /* FIXME _ProfStop(PGROUP_T1); */
1247 /* FIXME _ProfStart(PGROUP_RATE); */
1248 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1251 /* FIXME _ProfStop(PGROUP_RATE); */
1254 /*--------------TIER2------------------*/
1258 p_cstr_info->index_write = 1;
1260 /* FIXME _ProfStart(PGROUP_T2); */
1262 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1265 /* FIXME _ProfStop(PGROUP_T2); */
1267 /*---------------CLEAN-------------------*/
1272 OPJ_BOOL opj_tcd_decode_tile( opj_tcd_t *p_tcd,
1274 OPJ_UINT32 p_max_length,
1275 OPJ_UINT32 p_tile_no,
1276 opj_codestream_index_t *p_cstr_index,
1277 opj_event_mgr_t *p_manager
1280 OPJ_UINT32 l_data_read;
1281 p_tcd->tcd_tileno = p_tile_no;
1282 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1284 #ifdef TODO_MSD /* FIXME */
1287 OPJ_UINT32 resno, compno, numprec = 0;
1288 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1289 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1290 opj_tccp_t *tccp = &tcp->tccps[compno];
1291 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1292 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1293 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1294 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1295 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1296 numprec += res_idx->pw * res_idx->ph;
1297 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1298 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1301 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1302 p_cstr_info->packno = 0;
1307 /*--------------TIER2------------------*/
1308 /* FIXME _ProfStart(PGROUP_T2); */
1310 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index, p_manager))
1314 /* FIXME _ProfStop(PGROUP_T2); */
1316 /*------------------TIER1-----------------*/
1318 /* FIXME _ProfStart(PGROUP_T1); */
1320 (! opj_tcd_t1_decode(p_tcd))
1324 /* FIXME _ProfStop(PGROUP_T1); */
1326 /*----------------DWT---------------------*/
1328 /* FIXME _ProfStart(PGROUP_DWT); */
1330 (! opj_tcd_dwt_decode(p_tcd))
1334 /* FIXME _ProfStop(PGROUP_DWT); */
1336 /*----------------MCT-------------------*/
1337 /* FIXME _ProfStart(PGROUP_MCT); */
1339 (! opj_tcd_mct_decode(p_tcd, p_manager))
1343 /* FIXME _ProfStop(PGROUP_MCT); */
1345 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1347 (! opj_tcd_dc_level_shift_decode(p_tcd))
1351 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1354 /*---------------TILE-------------------*/
1358 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1360 OPJ_UINT32 p_dest_length
1363 OPJ_UINT32 i,j,k,l_data_size = 0;
1364 opj_image_comp_t * l_img_comp = 00;
1365 opj_tcd_tilecomp_t * l_tilec = 00;
1366 opj_tcd_resolution_t * l_res;
1367 OPJ_UINT32 l_size_comp, l_remaining;
1368 OPJ_UINT32 l_stride, l_width,l_height;
1370 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1371 if (l_data_size > p_dest_length) {
1375 l_tilec = p_tcd->tcd_image->tiles->comps;
1376 l_img_comp = p_tcd->image->comps;
1378 for (i=0;i<p_tcd->image->numcomps;++i) {
1379 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1380 l_remaining = l_img_comp->prec & 7; /* (%8) */
1381 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1382 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1383 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1384 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1390 if (l_size_comp == 3) {
1394 switch (l_size_comp)
1398 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1399 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1401 if (l_img_comp->sgnd) {
1402 for (j=0;j<l_height;++j) {
1403 for (k=0;k<l_width;++k) {
1404 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1406 l_src_ptr += l_stride;
1410 for (j=0;j<l_height;++j) {
1411 for (k=0;k<l_width;++k) {
1412 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1414 l_src_ptr += l_stride;
1418 p_dest = (OPJ_BYTE *)l_dest_ptr;
1423 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1424 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1426 if (l_img_comp->sgnd) {
1427 for (j=0;j<l_height;++j) {
1428 for (k=0;k<l_width;++k) {
1429 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1431 l_src_ptr += l_stride;
1435 for (j=0;j<l_height;++j) {
1436 for (k=0;k<l_width;++k) {
1437 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1439 l_src_ptr += l_stride;
1443 p_dest = (OPJ_BYTE*) l_dest_ptr;
1448 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1449 OPJ_INT32 * l_src_ptr = l_tilec->data;
1451 for (j=0;j<l_height;++j) {
1452 for (k=0;k<l_width;++k) {
1453 *(l_dest_ptr++) = (*(l_src_ptr++));
1455 l_src_ptr += l_stride;
1458 p_dest = (OPJ_BYTE*) l_dest_ptr;
1473 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1475 OPJ_UINT32 compno, resno, bandno, precno;
1476 opj_tcd_tile_t *l_tile = 00;
1477 opj_tcd_tilecomp_t *l_tile_comp = 00;
1478 opj_tcd_resolution_t *l_res = 00;
1479 opj_tcd_band_t *l_band = 00;
1480 opj_tcd_precinct_t *l_precinct = 00;
1481 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1482 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1488 if (! p_tcd->tcd_image) {
1492 if (p_tcd->m_is_decoder) {
1493 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1496 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1499 l_tile = p_tcd->tcd_image->tiles;
1504 l_tile_comp = l_tile->comps;
1506 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1507 l_res = l_tile_comp->resolutions;
1510 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1511 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1512 l_band = l_res->bands;
1513 for (bandno = 0; bandno < 3; ++bandno) {
1514 l_precinct = l_band->precincts;
1517 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1518 for (precno = 0; precno < l_nb_precincts; ++precno) {
1519 opj_tgt_destroy(l_precinct->incltree);
1520 l_precinct->incltree = 00;
1521 opj_tgt_destroy(l_precinct->imsbtree);
1522 l_precinct->imsbtree = 00;
1523 (*l_tcd_code_block_deallocate) (l_precinct);
1527 opj_free(l_band->precincts);
1528 l_band->precincts = 00;
1535 opj_free(l_tile_comp->resolutions);
1536 l_tile_comp->resolutions = 00;
1539 if (l_tile_comp->ownsData && l_tile_comp->data) {
1540 opj_aligned_free(l_tile_comp->data);
1541 l_tile_comp->data = 00;
1542 l_tile_comp->ownsData = 0;
1543 l_tile_comp->data_size = 0;
1544 l_tile_comp->data_size_needed = 0;
1549 opj_free(l_tile->comps);
1551 opj_free(p_tcd->tcd_image->tiles);
1552 p_tcd->tcd_image->tiles = 00;
1556 static OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1557 OPJ_BYTE * p_src_data,
1558 OPJ_UINT32 * p_data_read,
1559 OPJ_UINT32 p_max_src_size,
1560 opj_codestream_index_t *p_cstr_index,
1561 opj_event_mgr_t *p_manager
1566 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1571 if (! opj_t2_decode_packets(
1574 p_tcd->tcd_image->tiles,
1580 opj_t2_destroy(l_t2);
1584 opj_t2_destroy(l_t2);
1586 /*---------------CLEAN-------------------*/
1590 static OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1593 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1594 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1595 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1596 volatile OPJ_BOOL ret = OPJ_TRUE;
1598 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1599 opj_t1_decode_cblks(p_tcd->thread_pool, &ret, l_tile_comp, l_tccp);
1606 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1612 static OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1615 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1616 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1617 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1618 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1620 for (compno = 0; compno < l_tile->numcomps; compno++) {
1622 if (tcd->cp->reduce != 0) {
1623 tcd->image->comps[compno].resno_decoded =
1624 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1625 if (tcd->image->comps[compno].resno_decoded < 0)
1630 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1631 if(numres2decode > 0){
1634 if (l_tccp->qmfbid == 1) {
1635 if (! opj_dwt_decode(p_tcd->thread_pool, l_tile_comp, l_img_comp->resno_decoded+1)) {
1640 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1652 static OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1654 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1655 opj_tcp_t * l_tcp = p_tcd->tcp;
1656 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1657 OPJ_UINT32 l_samples,i;
1663 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1665 if (l_tile->numcomps >= 3 ){
1666 /* testcase 1336.pdf.asan.47.376 */
1667 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 - l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1668 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1669 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1670 opj_event_msg(p_manager, EVT_ERROR, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1673 else if (l_tcp->mct == 2) {
1676 if (! l_tcp->m_mct_decoding_matrix) {
1680 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1685 for (i=0;i<l_tile->numcomps;++i) {
1686 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1690 if (! opj_mct_decode_custom(/* MCT data */
1691 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1692 /* size of components */
1696 /* nb of components (i.e. size of pData) */
1698 /* tells if the data is signed */
1699 p_tcd->image->comps->sgnd)) {
1707 if (l_tcp->tccps->qmfbid == 1) {
1708 opj_mct_decode( l_tile->comps[0].data,
1709 l_tile->comps[1].data,
1710 l_tile->comps[2].data,
1714 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1715 (OPJ_FLOAT32*)l_tile->comps[1].data,
1716 (OPJ_FLOAT32*)l_tile->comps[2].data,
1722 opj_event_msg(p_manager, EVT_ERROR, "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1729 static OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1732 opj_tcd_tilecomp_t * l_tile_comp = 00;
1733 opj_tccp_t * l_tccp = 00;
1734 opj_image_comp_t * l_img_comp = 00;
1735 opj_tcd_resolution_t* l_res = 00;
1736 opj_tcd_tile_t * l_tile;
1737 OPJ_UINT32 l_width,l_height,i,j;
1738 OPJ_INT32 * l_current_ptr;
1739 OPJ_INT32 l_min, l_max;
1740 OPJ_UINT32 l_stride;
1742 l_tile = p_tcd->tcd_image->tiles;
1743 l_tile_comp = l_tile->comps;
1744 l_tccp = p_tcd->tcp->tccps;
1745 l_img_comp = p_tcd->image->comps;
1747 for (compno = 0; compno < l_tile->numcomps; compno++) {
1748 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1749 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1750 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1751 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1753 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1755 if (l_img_comp->sgnd) {
1756 l_min = -(1 << (l_img_comp->prec - 1));
1757 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1761 l_max = (1 << l_img_comp->prec) - 1;
1764 l_current_ptr = l_tile_comp->data;
1766 if (l_tccp->qmfbid == 1) {
1767 for (j=0;j<l_height;++j) {
1768 for (i = 0; i < l_width; ++i) {
1769 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1772 l_current_ptr += l_stride;
1776 for (j=0;j<l_height;++j) {
1777 for (i = 0; i < l_width; ++i) {
1778 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1779 *l_current_ptr = opj_int_clamp((OPJ_INT32)opj_lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1782 l_current_ptr += l_stride;
1797 * Deallocates the encoding data of the given precinct.
1799 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1801 OPJ_UINT32 cblkno , l_nb_code_blocks;
1803 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1805 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1806 /*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);*/
1807 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1808 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 );*/
1811 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1812 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1814 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1816 if (l_code_block->data) {
1817 opj_free(l_code_block->data);
1818 l_code_block->data = 00;
1821 if (l_code_block->segs) {
1822 opj_free(l_code_block->segs );
1823 l_code_block->segs = 00;
1829 opj_free(p_precinct->cblks.dec);
1830 p_precinct->cblks.dec = 00;
1835 * Deallocates the encoding data of the given precinct.
1837 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1839 OPJ_UINT32 cblkno , l_nb_code_blocks;
1841 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1843 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1845 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1846 if (l_code_block->data) {
1847 opj_free(l_code_block->data - 1);
1848 l_code_block->data = 00;
1851 if (l_code_block->layers) {
1852 opj_free(l_code_block->layers );
1853 l_code_block->layers = 00;
1856 if (l_code_block->passes) {
1857 opj_free(l_code_block->passes );
1858 l_code_block->passes = 00;
1863 opj_free(p_precinct->cblks.enc);
1865 p_precinct->cblks.enc = 00;
1869 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1871 OPJ_UINT32 i,l_data_size = 0;
1872 opj_image_comp_t * l_img_comp = 00;
1873 opj_tcd_tilecomp_t * l_tilec = 00;
1874 OPJ_UINT32 l_size_comp, l_remaining;
1876 l_tilec = p_tcd->tcd_image->tiles->comps;
1877 l_img_comp = p_tcd->image->comps;
1878 for (i=0;i<p_tcd->image->numcomps;++i) {
1879 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1880 l_remaining = l_img_comp->prec & 7; /* (%8) */
1886 if (l_size_comp == 3) {
1890 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1898 static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1901 opj_tcd_tilecomp_t * l_tile_comp = 00;
1902 opj_tccp_t * l_tccp = 00;
1903 opj_image_comp_t * l_img_comp = 00;
1904 opj_tcd_tile_t * l_tile;
1905 OPJ_UINT32 l_nb_elem,i;
1906 OPJ_INT32 * l_current_ptr;
1908 l_tile = p_tcd->tcd_image->tiles;
1909 l_tile_comp = l_tile->comps;
1910 l_tccp = p_tcd->tcp->tccps;
1911 l_img_comp = p_tcd->image->comps;
1913 for (compno = 0; compno < l_tile->numcomps; compno++) {
1914 l_current_ptr = l_tile_comp->data;
1915 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1917 if (l_tccp->qmfbid == 1) {
1918 for (i = 0; i < l_nb_elem; ++i) {
1919 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1924 for (i = 0; i < l_nb_elem; ++i) {
1925 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
1938 static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1940 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1941 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1942 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1944 OPJ_BYTE ** l_data = 00;
1945 opj_tcp_t * l_tcp = p_tcd->tcp;
1947 if(!p_tcd->tcp->mct) {
1951 if (p_tcd->tcp->mct == 2) {
1952 if (! p_tcd->tcp->m_mct_coding_matrix) {
1956 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1961 for (i=0;i<l_tile->numcomps;++i) {
1962 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1966 if (! opj_mct_encode_custom(/* MCT data */
1967 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1968 /* size of components */
1972 /* nb of components (i.e. size of pData) */
1974 /* tells if the data is signed */
1975 p_tcd->image->comps->sgnd) )
1983 else if (l_tcp->tccps->qmfbid == 0) {
1984 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1987 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1993 static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1995 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1996 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1997 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2000 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2001 if (l_tccp->qmfbid == 1) {
2002 if (! opj_dwt_encode(l_tile_comp)) {
2006 else if (l_tccp->qmfbid == 0) {
2007 if (! opj_dwt_encode_real(l_tile_comp)) {
2019 static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
2022 const OPJ_FLOAT64 * l_mct_norms;
2023 OPJ_UINT32 l_mct_numcomps = 0U;
2024 opj_tcp_t * l_tcp = p_tcd->tcp;
2026 l_t1 = opj_t1_create(OPJ_TRUE);
2031 if (l_tcp->mct == 1) {
2032 l_mct_numcomps = 3U;
2033 /* irreversible encoding */
2034 if (l_tcp->tccps->qmfbid == 0) {
2035 l_mct_norms = opj_mct_get_mct_norms_real();
2038 l_mct_norms = opj_mct_get_mct_norms();
2042 l_mct_numcomps = p_tcd->image->numcomps;
2043 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
2046 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms, l_mct_numcomps)) {
2047 opj_t1_destroy(l_t1);
2051 opj_t1_destroy(l_t1);
2056 static OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
2057 OPJ_BYTE * p_dest_data,
2058 OPJ_UINT32 * p_data_written,
2059 OPJ_UINT32 p_max_dest_size,
2060 opj_codestream_info_t *p_cstr_info )
2064 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2069 if (! opj_t2_encode_packets(
2072 p_tcd->tcd_image->tiles,
2073 p_tcd->tcp->numlayers,
2083 opj_t2_destroy(l_t2);
2087 opj_t2_destroy(l_t2);
2089 /*---------------CLEAN-------------------*/
2094 static OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
2095 OPJ_BYTE * p_dest_data,
2096 OPJ_UINT32 p_max_dest_size,
2097 opj_codestream_info_t *p_cstr_info )
2099 opj_cp_t * l_cp = p_tcd->cp;
2100 OPJ_UINT32 l_nb_written = 0;
2103 p_cstr_info->index_write = 0;
2106 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
2108 /* Normal Rate/distortion allocation */
2109 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2114 /* Fixed layer allocation */
2115 opj_tcd_rateallocate_fixed(p_tcd);
2122 OPJ_BOOL opj_tcd_copy_tile_data ( opj_tcd_t *p_tcd,
2124 OPJ_UINT32 p_src_length )
2126 OPJ_UINT32 i,j,l_data_size = 0;
2127 opj_image_comp_t * l_img_comp = 00;
2128 opj_tcd_tilecomp_t * l_tilec = 00;
2129 OPJ_UINT32 l_size_comp, l_remaining;
2130 OPJ_UINT32 l_nb_elem;
2132 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2133 if (l_data_size != p_src_length) {
2137 l_tilec = p_tcd->tcd_image->tiles->comps;
2138 l_img_comp = p_tcd->image->comps;
2139 for (i=0;i<p_tcd->image->numcomps;++i) {
2140 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2141 l_remaining = l_img_comp->prec & 7; /* (%8) */
2142 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2148 if (l_size_comp == 3) {
2152 switch (l_size_comp) {
2155 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2156 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2158 if (l_img_comp->sgnd) {
2159 for (j=0;j<l_nb_elem;++j) {
2160 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2164 for (j=0;j<l_nb_elem;++j) {
2165 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2169 p_src = (OPJ_BYTE*) l_src_ptr;
2174 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2175 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2177 if (l_img_comp->sgnd) {
2178 for (j=0;j<l_nb_elem;++j) {
2179 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2183 for (j=0;j<l_nb_elem;++j) {
2184 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2188 p_src = (OPJ_BYTE*) l_src_ptr;
2193 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2194 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2196 for (j=0;j<l_nb_elem;++j) {
2197 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2200 p_src = (OPJ_BYTE*) l_src_ptr;