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