Use thread pool for DWT decoding
[openjpeg.git] / src / lib / openjp2 / tcd.c
1 /*
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.
6  *
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.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
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.
27  *
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.
39  */
40
41 #include "opj_includes.h"
42
43 /* ----------------------------------------------------------------------- */
44
45 /* TODO MSD: */
46 #ifdef TODO_MSD 
47 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
48         int tileno, compno, resno, bandno, precno;/*, cblkno;*/
49
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);
53
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");
62                         fprintf(fd,
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");
68                                 fprintf(fd,
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");
74                                         fprintf(fd,
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");
80                                                 fprintf(fd,
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);
83                                                 /*
84                                                 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
85                                                         opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
86                                                         fprintf(fd, "            cblk {\n");
87                                                         fprintf(fd,
88                                                                 "              x0=%d, y0=%d, x1=%d, y1=%d\n",
89                                                                 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
90                                                         fprintf(fd, "            }\n");
91                                                 }
92                                                 */
93                                                 fprintf(fd, "          }\n");
94                                         }
95                                         fprintf(fd, "        }\n");
96                                 }
97                                 fprintf(fd, "      }\n");
98                         }
99                         fprintf(fd, "    }\n");
100                 }
101                 fprintf(fd, "  }\n");
102         }
103         fprintf(fd, "}\n");
104 }
105 #endif
106
107 /**
108  * Initializes tile coding/decoding
109  */
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);
111
112 /**
113 * Allocates memory for a decoding code block.
114 */
115 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
116
117 /**
118  * Deallocates the decoding data of the given precinct.
119  */
120 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
121
122 /**
123  * Allocates memory for an encoding code block (but not data).
124  */
125 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
126
127 /**
128  * Allocates data for an encoding code block
129  */
130 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block);
131
132 /**
133  * Deallocates the encoding data of the given precinct.
134  */
135 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
136
137
138 /**
139 Free the memory allocated for encoding
140 @param tcd TCD handle
141 */
142 static void opj_tcd_free_tile(opj_tcd_t *tcd);
143
144
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);
151
152 static OPJ_BOOL opj_tcd_t1_decode (opj_tcd_t *p_tcd);
153
154 static OPJ_BOOL opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
155
156 static OPJ_BOOL opj_tcd_mct_decode (opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager);
157
158 static OPJ_BOOL opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
159
160
161 static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
162
163 static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
164
165 static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
166
167 static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
168
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 );
174
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 );
179
180 /* ----------------------------------------------------------------------- */
181
182 /**
183 Create a new TCD handle
184 */
185 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
186 {
187         opj_tcd_t *l_tcd = 00;
188
189         /* create the tcd structure */
190         l_tcd = (opj_tcd_t*) opj_calloc(1,sizeof(opj_tcd_t));
191         if (!l_tcd) {
192                 return 00;
193         }
194
195         l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
196
197         l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1,sizeof(opj_tcd_image_t));
198         if (!l_tcd->tcd_image) {
199                 opj_free(l_tcd);
200                 return 00;
201         }
202
203         return l_tcd;
204 }
205
206
207 /* ----------------------------------------------------------------------- */
208
209 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
210         OPJ_UINT32 layno;
211
212         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
213                 opj_tcd_makelayer_fixed(tcd, layno, 1);
214         }
215 }
216
217
218 void opj_tcd_makelayer( opj_tcd_t *tcd,
219                                                 OPJ_UINT32 layno,
220                                                 OPJ_FLOAT64 thresh,
221                                                 OPJ_UINT32 final)
222 {
223         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
224         OPJ_UINT32 passno;
225
226         opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
227
228         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
229
230         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
231                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
232
233                 for (resno = 0; resno < tilec->numresolutions; resno++) {
234                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
235
236                         for (bandno = 0; bandno < res->numbands; bandno++) {
237                                 opj_tcd_band_t *band = &res->bands[bandno];
238
239                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
240                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
241
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];
245                                                 OPJ_UINT32 n;
246
247                                                 if (layno == 0) {
248                                                         cblk->numpassesinlayers = 0;
249                                                 }
250
251                                                 n = cblk->numpassesinlayers;
252
253                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
254                                                         OPJ_UINT32 dr;
255                                                         OPJ_FLOAT64 dd;
256                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
257
258                                                         if (n == 0) {
259                                                                 dr = pass->rate;
260                                                                 dd = pass->distortiondec;
261                                                         } else {
262                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
263                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
264                                                         }
265
266                                                         if (!dr) {
267                                                                 if (dd != 0)
268                                                                         n = passno + 1;
269                                                                 continue;
270                                                         }
271                                                         if (thresh - (dd / dr) < DBL_EPSILON) /* do not rely on float equality, check with DBL_EPSILON margin */
272                                                                 n = passno + 1;
273                                                 }
274
275                                                 layer->numpasses = n - cblk->numpassesinlayers;
276
277                                                 if (!layer->numpasses) {
278                                                         layer->disto = 0;
279                                                         continue;
280                                                 }
281
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;
286                                                 } else {
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;
290                                                 }
291
292                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
293
294                                                 if (final)
295                                                         cblk->numpassesinlayers = n;
296                                         }
297                                 }
298                         }
299                 }
300         }
301 }
302
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];
307         OPJ_UINT32 i, j, k;
308
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;
312
313         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
314                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
315
316                 for (i = 0; i < tcd_tcp->numlayers; i++) {
317                         for (j = 0; j < tilec->numresolutions; j++) {
318                                 for (k = 0; k < 3; k++) {
319                                         matrice[i][j][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));
322                                 }
323                         }
324                 }
325
326                 for (resno = 0; resno < tilec->numresolutions; resno++) {
327                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
328
329                         for (bandno = 0; bandno < res->numbands; bandno++) {
330                                 opj_tcd_band_t *band = &res->bands[bandno];
331
332                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
333                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
334
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];
338                                                 OPJ_UINT32 n;
339                                                 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
340
341                                                 /* Correction of the matrix of coefficient to include the IMSB information */
342                                                 if (layno == 0) {
343                                                         value = matrice[layno][resno][bandno];
344                                                         if (imsb >= value) {
345                                                                 value = 0;
346                                                         } else {
347                                                                 value -= imsb;
348                                                         }
349                                                 } else {
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]);
353                                                                 if (value < 0) {
354                                                                         value = 0;
355                                                                 }
356                                                         }
357                                                 }
358
359                                                 if (layno == 0) {
360                                                         cblk->numpassesinlayers = 0;
361                                                 }
362
363                                                 n = cblk->numpassesinlayers;
364                                                 if (cblk->numpassesinlayers == 0) {
365                                                         if (value != 0) {
366                                                                 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
367                                                         } else {
368                                                                 n = cblk->numpassesinlayers;
369                                                         }
370                                                 } else {
371                                                         n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
372                                                 }
373
374                                                 layer->numpasses = n - cblk->numpassesinlayers;
375
376                                                 if (!layer->numpasses)
377                                                         continue;
378
379                                                 if (cblk->numpassesinlayers == 0) {
380                                                         layer->len = cblk->passes[n - 1].rate;
381                                                         layer->data = cblk->data;
382                                                 } else {
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;
385                                                 }
386
387                                                 if (final)
388                                                         cblk->numpassesinlayers = n;
389                                         }
390                                 }
391                         }
392                 }
393         }
394 }
395
396 OPJ_BOOL opj_tcd_rateallocate(  opj_tcd_t *tcd,
397                                                                 OPJ_BYTE *dest,
398                                                                 OPJ_UINT32 * p_data_written,
399                                                                 OPJ_UINT32 len,
400                                                                 opj_codestream_info_t *cstr_info)
401 {
402         OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
403         OPJ_UINT32 passno;
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;
408
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;
412
413         min = DBL_MAX;
414         max = 0;
415
416         tcd_tile->numpix = 0;           /* fixed_quality */
417
418         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
419                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
420                 tilec->numpix = 0;
421
422                 for (resno = 0; resno < tilec->numresolutions; resno++) {
423                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
424
425                         for (bandno = 0; bandno < res->numbands; bandno++) {
426                                 opj_tcd_band_t *band = &res->bands[bandno];
427
428                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
429                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
430
431                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
432                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
433
434                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
435                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
436                                                         OPJ_INT32 dr;
437                                                         OPJ_FLOAT64 dd, rdslope;
438
439                                                         if (passno == 0) {
440                                                                 dr = (OPJ_INT32)pass->rate;
441                                                                 dd = pass->distortiondec;
442                                                         } else {
443                                                                 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
444                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
445                                                         }
446
447                                                         if (dr == 0) {
448                                                                 continue;
449                                                         }
450
451                                                         rdslope = dd / dr;
452                                                         if (rdslope < min) {
453                                                                 min = rdslope;
454                                                         }
455
456                                                         if (rdslope > max) {
457                                                                 max = rdslope;
458                                                         }
459                                                 } /* passno */
460
461                                                 /* fixed_quality */
462                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
463                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
464                                         } /* cbklno */
465                                 } /* precno */
466                         } /* bandno */
467                 } /* resno */
468
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));
472         } /* compno */
473
474         /* index file */
475         if(cstr_info) {
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 */
482                         return OPJ_FALSE;
483                 }
484         }
485
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;
492                 OPJ_UINT32 i;
493                 OPJ_FLOAT64 distotarget;                /* fixed_quality */
494
495                 /* fixed_quality */
496                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
497
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;
505
506                         if (t2 == 00) {
507                                 return OPJ_FALSE;
508                         }
509
510                         for     (i = 0; i < 128; ++i) {
511                                 OPJ_FLOAT64 distoachieved = 0;  /* fixed_quality */
512
513                                 thresh = (lo + hi) / 2;
514
515                                 opj_tcd_makelayer(tcd, layno, thresh, 0);
516
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)) {
520
521                                                         lo = thresh;
522                                                         continue;
523                                                 }
524                                                 else {
525                                                         distoachieved = layno == 0 ?
526                                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
527
528                                                         if (distoachieved < distotarget) {
529                                                                 hi=thresh;
530                                                                 stable_thresh = thresh;
531                                                                 continue;
532                                                         }else{
533                                                                 lo=thresh;
534                                                         }
535                                                 }
536                                         }else{
537                                                 distoachieved = (layno == 0) ?
538                                                                 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
539
540                                                 if (distoachieved < distotarget) {
541                                                         hi = thresh;
542                                                         stable_thresh = thresh;
543                                                         continue;
544                                                 }
545                                                 lo = thresh;
546                                         }
547                                 } else {
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))
549                                         {
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); */
552                                                 lo = thresh;
553                                                 continue;
554                                         }
555
556                                         hi = thresh;
557                                         stable_thresh = thresh;
558                                 }
559                         }
560
561                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
562
563                         opj_t2_destroy(t2);
564                 } else {
565                         goodthresh = min;
566                 }
567
568                 if(cstr_info) { /* Threshold for Marcela Index */
569                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
570                 }
571
572                 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
573
574                 /* fixed_quality */
575                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
576         }
577
578         return OPJ_TRUE;
579 }
580
581 OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
582                                            opj_image_t * p_image,
583                                            opj_cp_t * p_cp,
584                        opj_thread_pool_t* p_tp )
585 {
586         p_tcd->image = p_image;
587         p_tcd->cp = p_cp;
588
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) {
591                 return OPJ_FALSE;
592         }
593
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 ) {
596                 return OPJ_FALSE;
597         }
598
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;
602
603         return OPJ_TRUE;
604 }
605
606 /**
607 Destroy a previously created TCD handle
608 */
609 void opj_tcd_destroy(opj_tcd_t *tcd) {
610         if (tcd) {
611                 opj_tcd_free_tile(tcd);
612
613                 if (tcd->tcd_image) {
614                         opj_free(tcd->tcd_image);
615                         tcd->tcd_image = 00;
616                 }
617                 opj_free(tcd);
618         }
619 }
620
621 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
622 {
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 ) {
626                         return OPJ_FALSE;
627                 }
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;
631         }
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;
640                         return OPJ_FALSE;
641                 }
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;
645         }
646         return OPJ_TRUE;
647 }
648
649 /* ----------------------------------------------------------------------- */
650
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)
652 {
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;
666         OPJ_UINT32 p,q;
667         OPJ_UINT32 l_level_no;
668         OPJ_UINT32 l_pdx, l_pdy;
669         OPJ_UINT32 l_gain;
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;
684         
685         l_cp = p_tcd->cp;
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;
692         
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);*/
696         
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         l_ty0 = l_cp->ty0 + q * l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
702         l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
703         l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy), l_image->y1);
704
705         /* testcase 1888.pdf.asan.35.988 */
706         if (l_tccp->numresolutions == 0) {
707                 opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
708                 return OPJ_FALSE;
709         }
710         /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
711         
712         /*tile->numcomps = image->numcomps; */
713         for (compno = 0; compno < l_tile->numcomps; ++compno) {
714                 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
715                 l_image_comp->resno_decoded = 0;
716                 /* border of each l_tile component (global) */
717                 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
718                 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
719                 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
720                 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
721                 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
722                 
723                 /* compute l_data_size with overflow check */
724                 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
725                 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
726                 if ((l_data_size > 0U) && ((((OPJ_UINT32)-1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0))) {
727                         opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
728                         return OPJ_FALSE;
729                 }
730                 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
731                 
732                 if ((((OPJ_UINT32)-1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
733                         opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
734                         return OPJ_FALSE;
735                 }
736                 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
737                 l_tilec->numresolutions = l_tccp->numresolutions;
738                 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
739                         l_tilec->minimum_num_resolutions = 1;
740                 }
741                 else {
742                         l_tilec->minimum_num_resolutions = l_tccp->numresolutions - l_cp->m_specific_param.m_dec.m_reduce;
743                 }
744                 
745                 l_tilec->data_size_needed = l_data_size;
746                 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
747                         opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile data\n");
748                         return OPJ_FALSE;
749                 }
750                 
751                 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t);
752                 
753                 if (l_tilec->resolutions == 00) {
754                         l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
755                         if (! l_tilec->resolutions ) {
756                                 return OPJ_FALSE;
757                         }
758                         /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
759                         l_tilec->resolutions_size = l_data_size;
760                         memset(l_tilec->resolutions,0,l_data_size);
761                 }
762                 else if (l_data_size > l_tilec->resolutions_size) {
763                         opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size);
764                         if (! new_resolutions) {
765                                 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
766                                 opj_free(l_tilec->resolutions);
767                                 l_tilec->resolutions = NULL;
768                                 l_tilec->resolutions_size = 0;
769                                 return OPJ_FALSE;
770                         }
771                         l_tilec->resolutions = new_resolutions;
772                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
773                         memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size);
774                         l_tilec->resolutions_size = l_data_size;
775                 }
776                 
777                 l_level_no = l_tilec->numresolutions;
778                 l_res = l_tilec->resolutions;
779                 l_step_size = l_tccp->stepsizes;
780                 if (l_tccp->qmfbid == 0) {
781                         l_gain_ptr = &opj_dwt_getgain_real;
782                 }
783                 else {
784                         l_gain_ptr  = &opj_dwt_getgain;
785                 }
786                 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
787                 
788                 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
789                         /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
790                         OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
791                         OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
792                         OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
793                         
794                         --l_level_no;
795                         
796                         /* border for each resolution level (global) */
797                         l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
798                         l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
799                         l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
800                         l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
801                         /*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);*/
802                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
803                         l_pdx = l_tccp->prcw[resno];
804                         l_pdy = l_tccp->prch[resno];
805                         /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
806                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
807                         l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
808                         l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
809                         l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
810                         l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
811                         /*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 );*/
812                         
813                         l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
814                         l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
815                         /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
816                         
817                         l_nb_precincts = l_res->pw * l_res->ph;
818                         l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
819                         if (resno == 0) {
820                                 tlcbgxstart = l_tl_prc_x_start;
821                                 tlcbgystart = l_tl_prc_y_start;
822                                 /*brcbgxend = l_br_prc_x_end;*/
823                                 /* brcbgyend = l_br_prc_y_end;*/
824                                 cbgwidthexpn = l_pdx;
825                                 cbgheightexpn = l_pdy;
826                                 l_res->numbands = 1;
827                         }
828                         else {
829                                 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
830                                 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
831                                 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
832                                 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
833                                 cbgwidthexpn = l_pdx - 1;
834                                 cbgheightexpn = l_pdy - 1;
835                                 l_res->numbands = 3;
836                         }
837                         
838                         cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
839                         cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
840                         l_band = l_res->bands;
841                         
842                         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
843                                 OPJ_INT32 numbps;
844                                 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
845                                 
846                                 if (resno == 0) {
847                                         l_band->bandno = 0 ;
848                                         l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
849                                         l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
850                                         l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
851                                         l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
852                                 }
853                                 else {
854                                         l_band->bandno = bandno + 1;
855                                         /* x0b = 1 if bandno = 1 or 3 */
856                                         l_x0b = l_band->bandno&1;
857                                         /* y0b = 1 if bandno = 2 or 3 */
858                                         l_y0b = (OPJ_INT32)((l_band->bandno)>>1);
859                                         /* l_band border (global) */
860                                         l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b << l_level_no), (OPJ_INT32)(l_level_no + 1));
861                                         l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b << l_level_no), (OPJ_INT32)(l_level_no + 1));
862                                         l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b << l_level_no), (OPJ_INT32)(l_level_no + 1));
863                                         l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b << l_level_no), (OPJ_INT32)(l_level_no + 1));
864                                 }
865                                 
866                                 /** avoid an if with storing function pointer */
867                                 l_gain = (*l_gain_ptr) (l_band->bandno);
868                                 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
869                                 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * fraction;
870                                 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1;      /* WHY -1 ? */
871                                 
872                                 if (!l_band->precincts && (l_nb_precincts > 0U)) {
873                                         l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size);
874                                         if (! l_band->precincts) {
875                                                 return OPJ_FALSE;
876                                         }
877                                         /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size);     */
878                                         memset(l_band->precincts,0,l_nb_precinct_size);
879                                         l_band->precincts_data_size = l_nb_precinct_size;
880                                 }
881                                 else if (l_band->precincts_data_size < l_nb_precinct_size) {
882                                         
883                                         opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);
884                                         if (! new_precincts) {
885                                                 opj_event_msg(manager, EVT_ERROR, "Not enough memory to handle band precints\n");
886                                                 opj_free(l_band->precincts);
887                                                 l_band->precincts = NULL;
888                                                 l_band->precincts_data_size = 0;
889                                                 return OPJ_FALSE;
890                                         }
891                                         l_band->precincts = new_precincts;
892                                         /*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);*/
893                                         memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);
894                                         l_band->precincts_data_size = l_nb_precinct_size;
895                                 }
896                                 
897                                 l_current_precinct = l_band->precincts;
898                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
899                                         OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
900                                         OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn);
901                                         OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn);
902                                         OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
903                                         OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
904                                         /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
905                                         /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
906                                         
907                                         /* precinct size (global) */
908                                         /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
909                                         
910                                         l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
911                                         l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
912                                         l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
913                                         l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
914                                         /*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);*/
915                                         
916                                         tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
917                                         /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
918                                         tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
919                                         /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
920                                         brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
921                                         /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
922                                         brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
923                                         /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
924                                         l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn);
925                                         l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn);
926                                         
927                                         l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
928                                         /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */
929                                         l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
930                                         
931                                         if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
932                                                 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
933                                                 if (! l_current_precinct->cblks.blocks ) {
934                                                         return OPJ_FALSE;
935                                                 }
936                                                 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
937                                                 
938                                                 memset(l_current_precinct->cblks.blocks,0,l_nb_code_blocks_size);
939                                                 
940                                                 l_current_precinct->block_size = l_nb_code_blocks_size;
941                                         }
942                                         else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
943                                                 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks, l_nb_code_blocks_size);
944                                                 if (! new_blocks) {
945                                                         opj_free(l_current_precinct->cblks.blocks);
946                                                         l_current_precinct->cblks.blocks = NULL;
947                                                         l_current_precinct->block_size = 0;
948                                                         opj_event_msg(manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n");
949                                                         return OPJ_FALSE;
950                                                 }
951                                                 l_current_precinct->cblks.blocks = new_blocks;
952                                                 /*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);     */
953                                                 
954                                                 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) + l_current_precinct->block_size
955                                                                          ,0
956                                                                          ,l_nb_code_blocks_size - l_current_precinct->block_size);
957                                                 
958                                                 l_current_precinct->block_size = l_nb_code_blocks_size;
959                                         }
960                                         
961                                         if (! l_current_precinct->incltree) {
962                                                 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, l_current_precinct->ch, manager);
963                                         }
964                                         else{
965                                                 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, l_current_precinct->cw, l_current_precinct->ch, manager);
966                                         }
967
968                                         if (! l_current_precinct->incltree)     {
969                                                 opj_event_msg(manager, EVT_WARNING, "No incltree created.\n");
970                                                 /*return OPJ_FALSE;*/
971                                         }
972
973                                         if (! l_current_precinct->imsbtree) {
974                                                 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw, l_current_precinct->ch, manager);
975                                         }
976                                         else {
977                                                 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree, l_current_precinct->cw, l_current_precinct->ch, manager);
978                                         }
979
980                                         if (! l_current_precinct->imsbtree) {
981                                                 opj_event_msg(manager, EVT_WARNING, "No imsbtree created.\n");
982                                                 /*return OPJ_FALSE;*/
983                                         }
984
985                                         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
986                                                 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);
987                                                 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);
988                                                 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
989                                                 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
990                                                 
991                                                 if (isEncoder) {
992                                                         opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
993                                                         
994                                                         if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
995                                                                 return OPJ_FALSE;
996                                                         }
997                                                         /* code-block size (global) */
998                                                         l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
999                                                         l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1000                                                         l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1001                                                         l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1002                                                         
1003                                                         if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1004                                                                 return OPJ_FALSE;
1005                                                         }
1006                                                 } else {
1007                                                         opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1008                                                         
1009                                                         if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1010                                                                 return OPJ_FALSE;
1011                                                         }
1012                                                         /* code-block size (global) */
1013                                                         l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1014                                                         l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1015                                                         l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1016                                                         l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1017                                                 }
1018                                         }
1019                                         ++l_current_precinct;
1020                                 } /* precno */
1021                                 ++l_band;
1022                                 ++l_step_size;
1023                         } /* bandno */
1024                         ++l_res;
1025                 } /* resno */
1026                 ++l_tccp;
1027                 ++l_tilec;
1028                 ++l_image_comp;
1029         } /* compno */
1030         return OPJ_TRUE;
1031 }
1032
1033 OPJ_BOOL opj_tcd_init_encode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, opj_event_mgr_t* p_manager)
1034 {
1035         return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F, sizeof(opj_tcd_cblk_enc_t), p_manager);
1036 }
1037
1038 OPJ_BOOL opj_tcd_init_decode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, opj_event_mgr_t* p_manager)
1039 {
1040         return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F, sizeof(opj_tcd_cblk_dec_t), p_manager);
1041 }
1042
1043 /**
1044  * Allocates memory for an encoding code block (but not data memory).
1045  */
1046 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1047 {
1048         if (! p_code_block->layers) {
1049                 /* no memset since data */
1050                 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
1051                 if (! p_code_block->layers) {
1052                         return OPJ_FALSE;
1053                 }
1054         }
1055         if (! p_code_block->passes) {
1056                 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
1057                 if (! p_code_block->passes) {
1058                         return OPJ_FALSE;
1059                 }
1060         }
1061         return OPJ_TRUE;
1062 }
1063
1064 /**
1065  * Allocates data memory for an encoding code block.
1066  */
1067 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block)
1068 {
1069         OPJ_UINT32 l_data_size;
1070         
1071         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));
1072         
1073         if (l_data_size > p_code_block->data_size) {
1074                 if (p_code_block->data) {
1075                         opj_free(p_code_block->data - 1); /* again, why -1 */
1076                 }
1077                 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size+1);
1078                 if(! p_code_block->data) {
1079                         p_code_block->data_size = 0U;
1080                         return OPJ_FALSE;
1081                 }
1082                 p_code_block->data_size = l_data_size;
1083                 
1084                 p_code_block->data[0] = 0;
1085                 p_code_block->data+=1;   /*why +1 ?*/
1086         }
1087         return OPJ_TRUE;
1088 }
1089
1090 /**
1091  * Allocates memory for a decoding code block.
1092  */
1093 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1094 {
1095         if (! p_code_block->data) {
1096
1097                 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1098                 if (! p_code_block->data) {
1099                         return OPJ_FALSE;
1100                 }
1101                 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1102                 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1103
1104                 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,sizeof(opj_tcd_seg_t));
1105                 if (! p_code_block->segs) {
1106                         return OPJ_FALSE;
1107                 }
1108                 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1109
1110                 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1111                 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1112         } else {
1113                                         /* sanitize */
1114                                         OPJ_BYTE* l_data = p_code_block->data;
1115                                         OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1116                                         opj_tcd_seg_t * l_segs = p_code_block->segs;
1117                                         OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1118
1119                                         memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1120                                         p_code_block->data = l_data;
1121                                         p_code_block->data_max_size = l_data_max_size;
1122                                         p_code_block->segs = l_segs;
1123                                         p_code_block->m_current_max_segs = l_current_max_segs;
1124                                 }
1125
1126         return OPJ_TRUE;
1127 }
1128
1129 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1130 {
1131         OPJ_UINT32 i;
1132         OPJ_UINT32 l_data_size = 0;
1133         opj_image_comp_t * l_img_comp = 00;
1134         opj_tcd_tilecomp_t * l_tile_comp = 00;
1135         opj_tcd_resolution_t * l_res = 00;
1136         OPJ_UINT32 l_size_comp, l_remaining;
1137
1138         l_tile_comp = p_tcd->tcd_image->tiles->comps;
1139         l_img_comp = p_tcd->image->comps;
1140
1141         for (i=0;i<p_tcd->image->numcomps;++i) {
1142                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1143                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1144
1145                 if(l_remaining) {
1146                         ++l_size_comp;
1147                 }
1148
1149                 if (l_size_comp == 3) {
1150                         l_size_comp = 4;
1151                 }
1152
1153                 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1154                 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1155                 ++l_img_comp;
1156                 ++l_tile_comp;
1157         }
1158
1159         return l_data_size;
1160 }
1161
1162 OPJ_BOOL opj_tcd_encode_tile(   opj_tcd_t *p_tcd,
1163                                                         OPJ_UINT32 p_tile_no,
1164                                                         OPJ_BYTE *p_dest,
1165                                                         OPJ_UINT32 * p_data_written,
1166                                                         OPJ_UINT32 p_max_length,
1167                                                         opj_codestream_info_t *p_cstr_info)
1168 {
1169
1170         if (p_tcd->cur_tp_num == 0) {
1171
1172                 p_tcd->tcd_tileno = p_tile_no;
1173                 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1174
1175                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1176                 if(p_cstr_info)  {
1177                         OPJ_UINT32 l_num_packs = 0;
1178                         OPJ_UINT32 i;
1179                         opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1180                         opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1181
1182                         for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1183                                 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1184
1185                                 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1186                                 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1187
1188                                 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1189                                 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1190                                 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1191                         }
1192                         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));
1193                         if (!p_cstr_info->tile[p_tile_no].packet) {
1194                                 /* FIXME event manager error callback */
1195                                 return OPJ_FALSE;
1196                         }
1197                 }
1198                 /* << INDEX */
1199
1200                 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1201                 /*---------------TILE-------------------*/
1202                 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1203                         return OPJ_FALSE;
1204                 }
1205                 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1206
1207                 /* FIXME _ProfStart(PGROUP_MCT); */
1208                 if (! opj_tcd_mct_encode(p_tcd)) {
1209                         return OPJ_FALSE;
1210                 }
1211                 /* FIXME _ProfStop(PGROUP_MCT); */
1212
1213                 /* FIXME _ProfStart(PGROUP_DWT); */
1214                 if (! opj_tcd_dwt_encode(p_tcd)) {
1215                         return OPJ_FALSE;
1216                 }
1217                 /* FIXME  _ProfStop(PGROUP_DWT); */
1218
1219                 /* FIXME  _ProfStart(PGROUP_T1); */
1220                 if (! opj_tcd_t1_encode(p_tcd)) {
1221                         return OPJ_FALSE;
1222                 }
1223                 /* FIXME _ProfStop(PGROUP_T1); */
1224
1225                 /* FIXME _ProfStart(PGROUP_RATE); */
1226                 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1227                         return OPJ_FALSE;
1228                 }
1229                 /* FIXME _ProfStop(PGROUP_RATE); */
1230
1231         }
1232         /*--------------TIER2------------------*/
1233
1234         /* INDEX */
1235         if (p_cstr_info) {
1236                 p_cstr_info->index_write = 1;
1237         }
1238         /* FIXME _ProfStart(PGROUP_T2); */
1239
1240         if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1241                 return OPJ_FALSE;
1242         }
1243         /* FIXME _ProfStop(PGROUP_T2); */
1244
1245         /*---------------CLEAN-------------------*/
1246
1247         return OPJ_TRUE;
1248 }
1249
1250 OPJ_BOOL opj_tcd_decode_tile(   opj_tcd_t *p_tcd,
1251                                 OPJ_BYTE *p_src,
1252                                 OPJ_UINT32 p_max_length,
1253                                 OPJ_UINT32 p_tile_no,
1254                                 opj_codestream_index_t *p_cstr_index,
1255                                 opj_event_mgr_t *p_manager
1256                                 )
1257 {
1258         OPJ_UINT32 l_data_read;
1259         p_tcd->tcd_tileno = p_tile_no;
1260         p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1261
1262 #ifdef TODO_MSD /* FIXME */
1263         /* INDEX >>  */
1264         if(p_cstr_info) {
1265                 OPJ_UINT32 resno, compno, numprec = 0;
1266                 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1267                         opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1268                         opj_tccp_t *tccp = &tcp->tccps[compno];
1269                         opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1270                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1271                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1272                                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1273                                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1274                                 numprec += res_idx->pw * res_idx->ph;
1275                                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1276                                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1277                         }
1278                 }
1279                 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1280                 p_cstr_info->packno = 0;
1281         }
1282         /* << INDEX */
1283 #endif
1284
1285         /*--------------TIER2------------------*/
1286         /* FIXME _ProfStart(PGROUP_T2); */
1287         l_data_read = 0;
1288         if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index, p_manager))
1289         {
1290                 return OPJ_FALSE;
1291         }
1292         /* FIXME _ProfStop(PGROUP_T2); */
1293
1294         /*------------------TIER1-----------------*/
1295
1296         /* FIXME _ProfStart(PGROUP_T1); */
1297         if
1298                 (! opj_tcd_t1_decode(p_tcd))
1299         {
1300                 return OPJ_FALSE;
1301         }
1302         /* FIXME _ProfStop(PGROUP_T1); */
1303
1304         /*----------------DWT---------------------*/
1305
1306         /* FIXME _ProfStart(PGROUP_DWT); */
1307         if
1308                 (! opj_tcd_dwt_decode(p_tcd))
1309         {
1310                 return OPJ_FALSE;
1311         }
1312         /* FIXME _ProfStop(PGROUP_DWT); */
1313
1314         /*----------------MCT-------------------*/
1315         /* FIXME _ProfStart(PGROUP_MCT); */
1316         if
1317                 (! opj_tcd_mct_decode(p_tcd, p_manager))
1318         {
1319                 return OPJ_FALSE;
1320         }
1321         /* FIXME _ProfStop(PGROUP_MCT); */
1322
1323         /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1324         if
1325                 (! opj_tcd_dc_level_shift_decode(p_tcd))
1326         {
1327                 return OPJ_FALSE;
1328         }
1329         /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1330
1331
1332         /*---------------TILE-------------------*/
1333         return OPJ_TRUE;
1334 }
1335
1336 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1337                                     OPJ_BYTE * p_dest,
1338                                     OPJ_UINT32 p_dest_length
1339                                     )
1340 {
1341         OPJ_UINT32 i,j,k,l_data_size = 0;
1342         opj_image_comp_t * l_img_comp = 00;
1343         opj_tcd_tilecomp_t * l_tilec = 00;
1344         opj_tcd_resolution_t * l_res;
1345         OPJ_UINT32 l_size_comp, l_remaining;
1346         OPJ_UINT32 l_stride, l_width,l_height;
1347
1348         l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1349         if (l_data_size > p_dest_length) {
1350                 return OPJ_FALSE;
1351         }
1352
1353         l_tilec = p_tcd->tcd_image->tiles->comps;
1354         l_img_comp = p_tcd->image->comps;
1355
1356         for (i=0;i<p_tcd->image->numcomps;++i) {
1357                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1358                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1359                 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1360                 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1361                 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1362                 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1363
1364                 if (l_remaining) {
1365                         ++l_size_comp;
1366                 }
1367
1368                 if (l_size_comp == 3) {
1369                         l_size_comp = 4;
1370                 }
1371
1372                 switch (l_size_comp)
1373                         {
1374                         case 1:
1375                                 {
1376                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1377                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
1378
1379                                         if (l_img_comp->sgnd) {
1380                                                 for (j=0;j<l_height;++j) {
1381                                                         for (k=0;k<l_width;++k) {
1382                                                                 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1383                                                         }
1384                                                         l_src_ptr += l_stride;
1385                                                 }
1386                                         }
1387                                         else {
1388                                                 for (j=0;j<l_height;++j) {
1389                                                         for     (k=0;k<l_width;++k) {
1390                                                                 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1391                                                         }
1392                                                         l_src_ptr += l_stride;
1393                                                 }
1394                                         }
1395
1396                                         p_dest = (OPJ_BYTE *)l_dest_ptr;
1397                                 }
1398                                 break;
1399                         case 2:
1400                                 {
1401                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
1402                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1403
1404                                         if (l_img_comp->sgnd) {
1405                                                 for (j=0;j<l_height;++j) {
1406                                                         for (k=0;k<l_width;++k) {
1407                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1408                                                         }
1409                                                         l_src_ptr += l_stride;
1410                                                 }
1411                                         }
1412                                         else {
1413                                                 for (j=0;j<l_height;++j) {
1414                                                         for (k=0;k<l_width;++k) {
1415                                                                 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1416                                                         }
1417                                                         l_src_ptr += l_stride;
1418                                                 }
1419                                         }
1420
1421                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
1422                                 }
1423                                 break;
1424                         case 4:
1425                                 {
1426                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1427                                         OPJ_INT32 * l_src_ptr = l_tilec->data;
1428
1429                                         for (j=0;j<l_height;++j) {
1430                                                 for (k=0;k<l_width;++k) {
1431                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
1432                                                 }
1433                                                 l_src_ptr += l_stride;
1434                                         }
1435
1436                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
1437                                 }
1438                                 break;
1439                 }
1440
1441                 ++l_img_comp;
1442                 ++l_tilec;
1443         }
1444
1445         return OPJ_TRUE;
1446 }
1447
1448
1449
1450
1451 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1452 {
1453         OPJ_UINT32 compno, resno, bandno, precno;
1454         opj_tcd_tile_t *l_tile = 00;
1455         opj_tcd_tilecomp_t *l_tile_comp = 00;
1456         opj_tcd_resolution_t *l_res = 00;
1457         opj_tcd_band_t *l_band = 00;
1458         opj_tcd_precinct_t *l_precinct = 00;
1459         OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1460         void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1461
1462         if (! p_tcd) {
1463                 return;
1464         }
1465
1466         if (! p_tcd->tcd_image) {
1467                 return;
1468         }
1469
1470         if (p_tcd->m_is_decoder) {
1471                 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1472         }
1473         else {
1474                 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1475         }
1476
1477         l_tile = p_tcd->tcd_image->tiles;
1478         if (! l_tile) {
1479                 return;
1480         }
1481
1482         l_tile_comp = l_tile->comps;
1483
1484         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1485                 l_res = l_tile_comp->resolutions;
1486                 if (l_res) {
1487
1488                         l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1489                         for (resno = 0; resno < l_nb_resolutions; ++resno) {
1490                                 l_band = l_res->bands;
1491                                 for     (bandno = 0; bandno < 3; ++bandno) {
1492                                         l_precinct = l_band->precincts;
1493                                         if (l_precinct) {
1494
1495                                                 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1496                                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
1497                                                         opj_tgt_destroy(l_precinct->incltree);
1498                                                         l_precinct->incltree = 00;
1499                                                         opj_tgt_destroy(l_precinct->imsbtree);
1500                                                         l_precinct->imsbtree = 00;
1501                                                         (*l_tcd_code_block_deallocate) (l_precinct);
1502                                                         ++l_precinct;
1503                                                 }
1504
1505                                                 opj_free(l_band->precincts);
1506                                                 l_band->precincts = 00;
1507                                         }
1508                                         ++l_band;
1509                                 } /* for (resno */
1510                                 ++l_res;
1511                         }
1512
1513                         opj_free(l_tile_comp->resolutions);
1514                         l_tile_comp->resolutions = 00;
1515                 }
1516
1517                 if (l_tile_comp->ownsData && l_tile_comp->data) {
1518                         opj_aligned_free(l_tile_comp->data);
1519                         l_tile_comp->data = 00;
1520                         l_tile_comp->ownsData = 0;
1521                         l_tile_comp->data_size = 0;
1522                         l_tile_comp->data_size_needed = 0;
1523                 }
1524                 ++l_tile_comp;
1525         }
1526
1527         opj_free(l_tile->comps);
1528         l_tile->comps = 00;
1529         opj_free(p_tcd->tcd_image->tiles);
1530         p_tcd->tcd_image->tiles = 00;
1531 }
1532
1533
1534 static OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1535                             OPJ_BYTE * p_src_data,
1536                             OPJ_UINT32 * p_data_read,
1537                             OPJ_UINT32 p_max_src_size,
1538                             opj_codestream_index_t *p_cstr_index,
1539                             opj_event_mgr_t *p_manager
1540                             )
1541 {
1542         opj_t2_t * l_t2;
1543
1544         l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1545         if (l_t2 == 00) {
1546                 return OPJ_FALSE;
1547         }
1548
1549         if (! opj_t2_decode_packets(
1550                                         l_t2,
1551                                         p_tcd->tcd_tileno,
1552                                         p_tcd->tcd_image->tiles,
1553                                         p_src_data,
1554                                         p_data_read,
1555                                         p_max_src_size,
1556                                         p_cstr_index,
1557                                         p_manager)) {
1558                 opj_t2_destroy(l_t2);
1559                 return OPJ_FALSE;
1560         }
1561
1562         opj_t2_destroy(l_t2);
1563
1564         /*---------------CLEAN-------------------*/
1565         return OPJ_TRUE;
1566 }
1567
1568 static OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1569 {
1570         OPJ_UINT32 compno;
1571         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1572         opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1573         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1574         volatile OPJ_BOOL ret = OPJ_TRUE;
1575
1576         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1577                 opj_t1_decode_cblks(p_tcd->thread_pool, &ret, l_tile_comp, l_tccp);
1578                 if( !ret )
1579                     break;
1580                 ++l_tile_comp;
1581                 ++l_tccp;
1582         }
1583
1584         opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1585
1586         return ret;
1587 }
1588
1589
1590 static OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1591 {
1592         OPJ_UINT32 compno;
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         opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1597
1598         for (compno = 0; compno < l_tile->numcomps; compno++) {
1599                 /*
1600                 if (tcd->cp->reduce != 0) {
1601                         tcd->image->comps[compno].resno_decoded =
1602                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1603                         if (tcd->image->comps[compno].resno_decoded < 0)
1604                         {
1605                                 return false;
1606                         }
1607                 }
1608                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1609                 if(numres2decode > 0){
1610                 */
1611
1612                 if (l_tccp->qmfbid == 1) {
1613                         if (! opj_dwt_decode(p_tcd->thread_pool, l_tile_comp, l_img_comp->resno_decoded+1)) {
1614                                 return OPJ_FALSE;
1615                         }
1616                 }
1617                 else {
1618                         if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1619                                 return OPJ_FALSE;
1620                         }
1621                 }
1622
1623                 ++l_tile_comp;
1624                 ++l_img_comp;
1625                 ++l_tccp;
1626         }
1627
1628         return OPJ_TRUE;
1629 }
1630 static OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1631 {
1632         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1633         opj_tcp_t * l_tcp = p_tcd->tcp;
1634         opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1635         OPJ_UINT32 l_samples,i;
1636
1637         if (! l_tcp->mct) {
1638                 return OPJ_TRUE;
1639         }
1640
1641         l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1642
1643         if (l_tile->numcomps >= 3 ){
1644                 /* testcase 1336.pdf.asan.47.376 */
1645                 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 ||
1646                     (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1647                     (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1648                         opj_event_msg(p_manager, EVT_ERROR, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1649                         return OPJ_FALSE;
1650                 }
1651                 else if (l_tcp->mct == 2) {
1652                         OPJ_BYTE ** l_data;
1653
1654                         if (! l_tcp->m_mct_decoding_matrix) {
1655                                 return OPJ_TRUE;
1656                         }
1657
1658                         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1659                         if (! l_data) {
1660                                 return OPJ_FALSE;
1661                         }
1662
1663                         for (i=0;i<l_tile->numcomps;++i) {
1664                                 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1665                                 ++l_tile_comp;
1666                         }
1667
1668                         if (! opj_mct_decode_custom(/* MCT data */
1669                                                                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1670                                                                         /* size of components */
1671                                                                         l_samples,
1672                                                                         /* components */
1673                                                                         l_data,
1674                                                                         /* nb of components (i.e. size of pData) */
1675                                                                         l_tile->numcomps,
1676                                                                         /* tells if the data is signed */
1677                                                                         p_tcd->image->comps->sgnd)) {
1678                                 opj_free(l_data);
1679                                 return OPJ_FALSE;
1680                         }
1681
1682                         opj_free(l_data);
1683                 }
1684                 else {
1685                         if (l_tcp->tccps->qmfbid == 1) {
1686                                 opj_mct_decode(     l_tile->comps[0].data,
1687                                                         l_tile->comps[1].data,
1688                                                         l_tile->comps[2].data,
1689                                                         l_samples);
1690                         }
1691                         else {
1692                             opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1693                                                 (OPJ_FLOAT32*)l_tile->comps[1].data,
1694                                                 (OPJ_FLOAT32*)l_tile->comps[2].data,
1695                                                 l_samples);
1696                         }
1697                 }
1698         }
1699         else {
1700                 opj_event_msg(p_manager, EVT_ERROR, "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1701         }
1702
1703         return OPJ_TRUE;
1704 }
1705
1706
1707 static OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1708 {
1709         OPJ_UINT32 compno;
1710         opj_tcd_tilecomp_t * l_tile_comp = 00;
1711         opj_tccp_t * l_tccp = 00;
1712         opj_image_comp_t * l_img_comp = 00;
1713         opj_tcd_resolution_t* l_res = 00;
1714         opj_tcd_tile_t * l_tile;
1715         OPJ_UINT32 l_width,l_height,i,j;
1716         OPJ_INT32 * l_current_ptr;
1717         OPJ_INT32 l_min, l_max;
1718         OPJ_UINT32 l_stride;
1719
1720         l_tile = p_tcd->tcd_image->tiles;
1721         l_tile_comp = l_tile->comps;
1722         l_tccp = p_tcd->tcp->tccps;
1723         l_img_comp = p_tcd->image->comps;
1724
1725         for (compno = 0; compno < l_tile->numcomps; compno++) {
1726                 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1727                 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1728                 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1729                 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1730
1731                 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1732
1733                 if (l_img_comp->sgnd) {
1734                         l_min = -(1 << (l_img_comp->prec - 1));
1735                         l_max = (1 << (l_img_comp->prec - 1)) - 1;
1736                 }
1737                 else {
1738             l_min = 0;
1739                         l_max = (1 << l_img_comp->prec) - 1;
1740                 }
1741
1742                 l_current_ptr = l_tile_comp->data;
1743
1744                 if (l_tccp->qmfbid == 1) {
1745                         for (j=0;j<l_height;++j) {
1746                                 for (i = 0; i < l_width; ++i) {
1747                                         *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1748                                         ++l_current_ptr;
1749                                 }
1750                                 l_current_ptr += l_stride;
1751                         }
1752                 }
1753                 else {
1754                         for (j=0;j<l_height;++j) {
1755                                 for (i = 0; i < l_width; ++i) {
1756                                         OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1757                                         *l_current_ptr = opj_int_clamp((OPJ_INT32)opj_lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1758                                         ++l_current_ptr;
1759                                 }
1760                                 l_current_ptr += l_stride;
1761                         }
1762                 }
1763
1764                 ++l_img_comp;
1765                 ++l_tccp;
1766                 ++l_tile_comp;
1767         }
1768
1769         return OPJ_TRUE;
1770 }
1771
1772
1773
1774 /**
1775  * Deallocates the encoding data of the given precinct.
1776  */
1777 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1778 {
1779         OPJ_UINT32 cblkno , l_nb_code_blocks;
1780
1781         opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1782         if (l_code_block) {
1783                 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1784                 /*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);*/
1785                 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1786                                 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 );*/
1787
1788
1789                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1790                 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1791
1792                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1793
1794                         if (l_code_block->data) {
1795                                 opj_free(l_code_block->data);
1796                                 l_code_block->data = 00;
1797                         }
1798
1799                         if (l_code_block->segs) {
1800                                 opj_free(l_code_block->segs );
1801                                 l_code_block->segs = 00;
1802                         }
1803
1804                         ++l_code_block;
1805                 }
1806
1807                 opj_free(p_precinct->cblks.dec);
1808                 p_precinct->cblks.dec = 00;
1809         }
1810 }
1811
1812 /**
1813  * Deallocates the encoding data of the given precinct.
1814  */
1815 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1816 {       
1817         OPJ_UINT32 cblkno , l_nb_code_blocks;
1818
1819         opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1820         if (l_code_block) {
1821                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1822                 
1823                 for     (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
1824                         if (l_code_block->data) {
1825                                 opj_free(l_code_block->data - 1);
1826                                 l_code_block->data = 00;
1827                         }
1828
1829                         if (l_code_block->layers) {
1830                                 opj_free(l_code_block->layers );
1831                                 l_code_block->layers = 00;
1832                         }
1833
1834                         if (l_code_block->passes) {
1835                                 opj_free(l_code_block->passes );
1836                                 l_code_block->passes = 00;
1837                         }
1838                         ++l_code_block;
1839                 }
1840
1841                 opj_free(p_precinct->cblks.enc);
1842                 
1843                 p_precinct->cblks.enc = 00;
1844         }
1845 }
1846
1847 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1848 {
1849         OPJ_UINT32 i,l_data_size = 0;
1850         opj_image_comp_t * l_img_comp = 00;
1851         opj_tcd_tilecomp_t * l_tilec = 00;
1852         OPJ_UINT32 l_size_comp, l_remaining;
1853
1854         l_tilec = p_tcd->tcd_image->tiles->comps;
1855         l_img_comp = p_tcd->image->comps;
1856         for (i=0;i<p_tcd->image->numcomps;++i) {
1857                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1858                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1859
1860                 if (l_remaining) {
1861                         ++l_size_comp;
1862                 }
1863
1864                 if (l_size_comp == 3) {
1865                         l_size_comp = 4;
1866                 }
1867
1868                 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1869                 ++l_img_comp;
1870                 ++l_tilec;
1871         }
1872
1873         return l_data_size;
1874 }
1875                 
1876 static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1877 {
1878         OPJ_UINT32 compno;
1879         opj_tcd_tilecomp_t * l_tile_comp = 00;
1880         opj_tccp_t * l_tccp = 00;
1881         opj_image_comp_t * l_img_comp = 00;
1882         opj_tcd_tile_t * l_tile;
1883         OPJ_UINT32 l_nb_elem,i;
1884         OPJ_INT32 * l_current_ptr;
1885
1886         l_tile = p_tcd->tcd_image->tiles;
1887         l_tile_comp = l_tile->comps;
1888         l_tccp = p_tcd->tcp->tccps;
1889         l_img_comp = p_tcd->image->comps;
1890
1891         for (compno = 0; compno < l_tile->numcomps; compno++) {
1892                 l_current_ptr = l_tile_comp->data;
1893                 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1894
1895                 if (l_tccp->qmfbid == 1) {
1896                         for     (i = 0; i < l_nb_elem; ++i) {
1897                                 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1898                                 ++l_current_ptr;
1899                         }
1900                 }
1901                 else {
1902                         for (i = 0; i < l_nb_elem; ++i) {
1903                                 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
1904                                 ++l_current_ptr;
1905                         }
1906                 }
1907
1908                 ++l_img_comp;
1909                 ++l_tccp;
1910                 ++l_tile_comp;
1911         }
1912
1913         return OPJ_TRUE;
1914 }
1915
1916 static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1917 {
1918         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1919         opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1920         OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1921         OPJ_UINT32 i;
1922         OPJ_BYTE ** l_data = 00;
1923         opj_tcp_t * l_tcp = p_tcd->tcp;
1924
1925         if(!p_tcd->tcp->mct) {
1926                 return OPJ_TRUE;
1927         }
1928
1929         if (p_tcd->tcp->mct == 2) {
1930                 if (! p_tcd->tcp->m_mct_coding_matrix) {
1931                         return OPJ_TRUE;
1932                 }
1933
1934         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1935                 if (! l_data) {
1936                         return OPJ_FALSE;
1937                 }
1938
1939                 for (i=0;i<l_tile->numcomps;++i) {
1940                         l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1941                         ++l_tile_comp;
1942                 }
1943
1944                 if (! opj_mct_encode_custom(/* MCT data */
1945                                         (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1946                                         /* size of components */
1947                                         samples,
1948                                         /* components */
1949                                         l_data,
1950                                         /* nb of components (i.e. size of pData) */
1951                                         l_tile->numcomps,
1952                                         /* tells if the data is signed */
1953                                         p_tcd->image->comps->sgnd) )
1954                 {
1955             opj_free(l_data);
1956                         return OPJ_FALSE;
1957                 }
1958
1959                 opj_free(l_data);
1960         }
1961         else if (l_tcp->tccps->qmfbid == 0) {
1962                 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1963         }
1964         else {
1965                 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1966         }
1967
1968         return OPJ_TRUE;
1969 }
1970
1971 static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1972 {
1973         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1974         opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1975         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1976         OPJ_UINT32 compno;
1977
1978         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1979                 if (l_tccp->qmfbid == 1) {
1980                         if (! opj_dwt_encode(l_tile_comp)) {
1981                                 return OPJ_FALSE;
1982                         }
1983                 }
1984                 else if (l_tccp->qmfbid == 0) {
1985                         if (! opj_dwt_encode_real(l_tile_comp)) {
1986                                 return OPJ_FALSE;
1987                         }
1988                 }
1989
1990                 ++l_tile_comp;
1991                 ++l_tccp;
1992         }
1993
1994         return OPJ_TRUE;
1995 }
1996
1997 static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
1998 {
1999         opj_t1_t * l_t1;
2000         const OPJ_FLOAT64 * l_mct_norms;
2001         OPJ_UINT32 l_mct_numcomps = 0U;
2002         opj_tcp_t * l_tcp = p_tcd->tcp;
2003
2004         l_t1 = opj_t1_create(OPJ_TRUE);
2005         if (l_t1 == 00) {
2006                 return OPJ_FALSE;
2007         }
2008
2009         if (l_tcp->mct == 1) {
2010                 l_mct_numcomps = 3U;
2011                 /* irreversible encoding */
2012                 if (l_tcp->tccps->qmfbid == 0) {
2013                         l_mct_norms = opj_mct_get_mct_norms_real();
2014                 }
2015                 else {
2016                         l_mct_norms = opj_mct_get_mct_norms();
2017                 }
2018         }
2019         else {
2020                 l_mct_numcomps = p_tcd->image->numcomps;
2021                 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
2022         }
2023
2024         if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms, l_mct_numcomps)) {
2025         opj_t1_destroy(l_t1);
2026                 return OPJ_FALSE;
2027         }
2028
2029         opj_t1_destroy(l_t1);
2030
2031         return OPJ_TRUE;
2032 }
2033
2034 static OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
2035                                                 OPJ_BYTE * p_dest_data,
2036                                                 OPJ_UINT32 * p_data_written,
2037                                                 OPJ_UINT32 p_max_dest_size,
2038                                                 opj_codestream_info_t *p_cstr_info )
2039 {
2040         opj_t2_t * l_t2;
2041
2042         l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2043         if (l_t2 == 00) {
2044                 return OPJ_FALSE;
2045         }
2046
2047         if (! opj_t2_encode_packets(
2048                                         l_t2,
2049                                         p_tcd->tcd_tileno,
2050                                         p_tcd->tcd_image->tiles,
2051                                         p_tcd->tcp->numlayers,
2052                                         p_dest_data,
2053                                         p_data_written,
2054                                         p_max_dest_size,
2055                                         p_cstr_info,
2056                                         p_tcd->tp_num,
2057                                         p_tcd->tp_pos,
2058                                         p_tcd->cur_pino,
2059                                         FINAL_PASS))
2060         {
2061                 opj_t2_destroy(l_t2);
2062                 return OPJ_FALSE;
2063         }
2064
2065         opj_t2_destroy(l_t2);
2066
2067         /*---------------CLEAN-------------------*/
2068         return OPJ_TRUE;
2069 }
2070
2071
2072 static OPJ_BOOL opj_tcd_rate_allocate_encode(  opj_tcd_t *p_tcd,
2073                                                                             OPJ_BYTE * p_dest_data,
2074                                                                             OPJ_UINT32 p_max_dest_size,
2075                                                                             opj_codestream_info_t *p_cstr_info )
2076 {
2077         opj_cp_t * l_cp = p_tcd->cp;
2078         OPJ_UINT32 l_nb_written = 0;
2079
2080         if (p_cstr_info)  {
2081                 p_cstr_info->index_write = 0;
2082         }
2083
2084         if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality)  {
2085                 /* fixed_quality */
2086                 /* Normal Rate/distortion allocation */
2087                 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2088                         return OPJ_FALSE;
2089                 }
2090         }
2091         else {
2092                 /* Fixed layer allocation */
2093                 opj_tcd_rateallocate_fixed(p_tcd);
2094         }
2095
2096         return OPJ_TRUE;
2097 }
2098
2099
2100 OPJ_BOOL opj_tcd_copy_tile_data (       opj_tcd_t *p_tcd,
2101                                                                     OPJ_BYTE * p_src,
2102                                                                     OPJ_UINT32 p_src_length )
2103 {
2104         OPJ_UINT32 i,j,l_data_size = 0;
2105         opj_image_comp_t * l_img_comp = 00;
2106         opj_tcd_tilecomp_t * l_tilec = 00;
2107         OPJ_UINT32 l_size_comp, l_remaining;
2108         OPJ_UINT32 l_nb_elem;
2109
2110         l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2111         if (l_data_size != p_src_length) {
2112                 return OPJ_FALSE;
2113         }
2114
2115         l_tilec = p_tcd->tcd_image->tiles->comps;
2116         l_img_comp = p_tcd->image->comps;
2117         for (i=0;i<p_tcd->image->numcomps;++i) {
2118                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2119                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2120                 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2121
2122                 if (l_remaining) {
2123                         ++l_size_comp;
2124                 }
2125
2126                 if (l_size_comp == 3) {
2127                         l_size_comp = 4;
2128                 }
2129
2130                 switch (l_size_comp) {
2131                         case 1:
2132                                 {
2133                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2134                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2135
2136                                         if (l_img_comp->sgnd) {
2137                                                 for (j=0;j<l_nb_elem;++j) {
2138                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2139                                                 }
2140                                         }
2141                                         else {
2142                                                 for (j=0;j<l_nb_elem;++j) {
2143                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2144                                                 }
2145                                         }
2146
2147                                         p_src = (OPJ_BYTE*) l_src_ptr;
2148                                 }
2149                                 break;
2150                         case 2:
2151                                 {
2152                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2153                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2154
2155                                         if (l_img_comp->sgnd) {
2156                                                 for (j=0;j<l_nb_elem;++j) {
2157                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2158                                                 }
2159                                         }
2160                                         else {
2161                                                 for (j=0;j<l_nb_elem;++j) {
2162                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2163                                                 }
2164                                         }
2165
2166                                         p_src = (OPJ_BYTE*) l_src_ptr;
2167                                 }
2168                                 break;
2169                         case 4:
2170                                 {
2171                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2172                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2173
2174                                         for (j=0;j<l_nb_elem;++j) {
2175                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2176                                         }
2177
2178                                         p_src = (OPJ_BYTE*) l_src_ptr;
2179                                 }
2180                                 break;
2181                 }
2182
2183                 ++l_img_comp;
2184                 ++l_tilec;
2185         }
2186
2187         return OPJ_TRUE;
2188 }