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