Reformat whole codebase with astyle.options (#128)
[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             opj_free(p_code_block->data - 1); /* again, why -1 */
1166         }
1167         p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1168         if (! p_code_block->data) {
1169             p_code_block->data_size = 0U;
1170             return OPJ_FALSE;
1171         }
1172         p_code_block->data_size = l_data_size;
1173
1174         p_code_block->data[0] = 0;
1175         p_code_block->data += 1; /*why +1 ?*/
1176     }
1177     return OPJ_TRUE;
1178 }
1179
1180 /**
1181  * Allocates memory for a decoding code block.
1182  */
1183 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1184         p_code_block)
1185 {
1186     if (! p_code_block->data) {
1187
1188         p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1189         if (! p_code_block->data) {
1190             return OPJ_FALSE;
1191         }
1192         p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1193         /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1194
1195         p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1196                              sizeof(opj_tcd_seg_t));
1197         if (! p_code_block->segs) {
1198             return OPJ_FALSE;
1199         }
1200         /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1201
1202         p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1203         /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1204     } else {
1205         /* sanitize */
1206         OPJ_BYTE* l_data = p_code_block->data;
1207         OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1208         opj_tcd_seg_t * l_segs = p_code_block->segs;
1209         OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1210
1211         memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1212         p_code_block->data = l_data;
1213         p_code_block->data_max_size = l_data_max_size;
1214         p_code_block->segs = l_segs;
1215         p_code_block->m_current_max_segs = l_current_max_segs;
1216     }
1217
1218     return OPJ_TRUE;
1219 }
1220
1221 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd)
1222 {
1223     OPJ_UINT32 i;
1224     OPJ_UINT32 l_data_size = 0;
1225     opj_image_comp_t * l_img_comp = 00;
1226     opj_tcd_tilecomp_t * l_tile_comp = 00;
1227     opj_tcd_resolution_t * l_res = 00;
1228     OPJ_UINT32 l_size_comp, l_remaining;
1229
1230     l_tile_comp = p_tcd->tcd_image->tiles->comps;
1231     l_img_comp = p_tcd->image->comps;
1232
1233     for (i = 0; i < p_tcd->image->numcomps; ++i) {
1234         l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1235         l_remaining = l_img_comp->prec & 7;  /* (%8) */
1236
1237         if (l_remaining) {
1238             ++l_size_comp;
1239         }
1240
1241         if (l_size_comp == 3) {
1242             l_size_comp = 4;
1243         }
1244
1245         l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1246         l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) *
1247                        (l_res->y1 - l_res->y0));
1248         ++l_img_comp;
1249         ++l_tile_comp;
1250     }
1251
1252     return l_data_size;
1253 }
1254
1255 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1256                              OPJ_UINT32 p_tile_no,
1257                              OPJ_BYTE *p_dest,
1258                              OPJ_UINT32 * p_data_written,
1259                              OPJ_UINT32 p_max_length,
1260                              opj_codestream_info_t *p_cstr_info)
1261 {
1262
1263     if (p_tcd->cur_tp_num == 0) {
1264
1265         p_tcd->tcd_tileno = p_tile_no;
1266         p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1267
1268         /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1269         if (p_cstr_info)  {
1270             OPJ_UINT32 l_num_packs = 0;
1271             OPJ_UINT32 i;
1272             opj_tcd_tilecomp_t *l_tilec_idx =
1273                 &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1274             opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1275
1276             for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1277                 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1278
1279                 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1280                 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1281
1282                 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1283                 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1284                 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1285             }
1286             p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1287                     size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs,
1288                                                   sizeof(opj_packet_info_t));
1289             if (!p_cstr_info->tile[p_tile_no].packet) {
1290                 /* FIXME event manager error callback */
1291                 return OPJ_FALSE;
1292             }
1293         }
1294         /* << INDEX */
1295
1296         /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1297         /*---------------TILE-------------------*/
1298         if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1299             return OPJ_FALSE;
1300         }
1301         /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1302
1303         /* FIXME _ProfStart(PGROUP_MCT); */
1304         if (! opj_tcd_mct_encode(p_tcd)) {
1305             return OPJ_FALSE;
1306         }
1307         /* FIXME _ProfStop(PGROUP_MCT); */
1308
1309         /* FIXME _ProfStart(PGROUP_DWT); */
1310         if (! opj_tcd_dwt_encode(p_tcd)) {
1311             return OPJ_FALSE;
1312         }
1313         /* FIXME  _ProfStop(PGROUP_DWT); */
1314
1315         /* FIXME  _ProfStart(PGROUP_T1); */
1316         if (! opj_tcd_t1_encode(p_tcd)) {
1317             return OPJ_FALSE;
1318         }
1319         /* FIXME _ProfStop(PGROUP_T1); */
1320
1321         /* FIXME _ProfStart(PGROUP_RATE); */
1322         if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length, p_cstr_info)) {
1323             return OPJ_FALSE;
1324         }
1325         /* FIXME _ProfStop(PGROUP_RATE); */
1326
1327     }
1328     /*--------------TIER2------------------*/
1329
1330     /* INDEX */
1331     if (p_cstr_info) {
1332         p_cstr_info->index_write = 1;
1333     }
1334     /* FIXME _ProfStart(PGROUP_T2); */
1335
1336     if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1337                             p_cstr_info)) {
1338         return OPJ_FALSE;
1339     }
1340     /* FIXME _ProfStop(PGROUP_T2); */
1341
1342     /*---------------CLEAN-------------------*/
1343
1344     return OPJ_TRUE;
1345 }
1346
1347 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1348                              OPJ_BYTE *p_src,
1349                              OPJ_UINT32 p_max_length,
1350                              OPJ_UINT32 p_tile_no,
1351                              opj_codestream_index_t *p_cstr_index,
1352                              opj_event_mgr_t *p_manager
1353                             )
1354 {
1355     OPJ_UINT32 l_data_read;
1356     p_tcd->tcd_tileno = p_tile_no;
1357     p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1358
1359 #ifdef TODO_MSD /* FIXME */
1360     /* INDEX >>  */
1361     if (p_cstr_info) {
1362         OPJ_UINT32 resno, compno, numprec = 0;
1363         for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1364             opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1365             opj_tccp_t *tccp = &tcp->tccps[compno];
1366             opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1367             for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1368                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1369                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1370                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1371                 numprec += res_idx->pw * res_idx->ph;
1372                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1373                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1374             }
1375         }
1376         p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1377                 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1378         p_cstr_info->packno = 0;
1379     }
1380     /* << INDEX */
1381 #endif
1382
1383     /*--------------TIER2------------------*/
1384     /* FIXME _ProfStart(PGROUP_T2); */
1385     l_data_read = 0;
1386     if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1387                             p_manager)) {
1388         return OPJ_FALSE;
1389     }
1390     /* FIXME _ProfStop(PGROUP_T2); */
1391
1392     /*------------------TIER1-----------------*/
1393
1394     /* FIXME _ProfStart(PGROUP_T1); */
1395     if
1396     (! opj_tcd_t1_decode(p_tcd)) {
1397         return OPJ_FALSE;
1398     }
1399     /* FIXME _ProfStop(PGROUP_T1); */
1400
1401     /*----------------DWT---------------------*/
1402
1403     /* FIXME _ProfStart(PGROUP_DWT); */
1404     if
1405     (! opj_tcd_dwt_decode(p_tcd)) {
1406         return OPJ_FALSE;
1407     }
1408     /* FIXME _ProfStop(PGROUP_DWT); */
1409
1410     /*----------------MCT-------------------*/
1411     /* FIXME _ProfStart(PGROUP_MCT); */
1412     if
1413     (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1414         return OPJ_FALSE;
1415     }
1416     /* FIXME _ProfStop(PGROUP_MCT); */
1417
1418     /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1419     if
1420     (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1421         return OPJ_FALSE;
1422     }
1423     /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1424
1425
1426     /*---------------TILE-------------------*/
1427     return OPJ_TRUE;
1428 }
1429
1430 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1431                                   OPJ_BYTE * p_dest,
1432                                   OPJ_UINT32 p_dest_length
1433                                  )
1434 {
1435     OPJ_UINT32 i, j, k, l_data_size = 0;
1436     opj_image_comp_t * l_img_comp = 00;
1437     opj_tcd_tilecomp_t * l_tilec = 00;
1438     opj_tcd_resolution_t * l_res;
1439     OPJ_UINT32 l_size_comp, l_remaining;
1440     OPJ_UINT32 l_stride, l_width, l_height;
1441
1442     l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1443     if (l_data_size > p_dest_length) {
1444         return OPJ_FALSE;
1445     }
1446
1447     l_tilec = p_tcd->tcd_image->tiles->comps;
1448     l_img_comp = p_tcd->image->comps;
1449
1450     for (i = 0; i < p_tcd->image->numcomps; ++i) {
1451         l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1452         l_remaining = l_img_comp->prec & 7;  /* (%8) */
1453         l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1454         l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1455         l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1456         l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1457
1458         if (l_remaining) {
1459             ++l_size_comp;
1460         }
1461
1462         if (l_size_comp == 3) {
1463             l_size_comp = 4;
1464         }
1465
1466         switch (l_size_comp) {
1467         case 1: {
1468             OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1469             const OPJ_INT32 * l_src_ptr = l_tilec->data;
1470
1471             if (l_img_comp->sgnd) {
1472                 for (j = 0; j < l_height; ++j) {
1473                     for (k = 0; k < l_width; ++k) {
1474                         *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1475                     }
1476                     l_src_ptr += l_stride;
1477                 }
1478             } else {
1479                 for (j = 0; j < l_height; ++j) {
1480                     for (k = 0; k < l_width; ++k) {
1481                         *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1482                     }
1483                     l_src_ptr += l_stride;
1484                 }
1485             }
1486
1487             p_dest = (OPJ_BYTE *)l_dest_ptr;
1488         }
1489         break;
1490         case 2: {
1491             const OPJ_INT32 * l_src_ptr = l_tilec->data;
1492             OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1493
1494             if (l_img_comp->sgnd) {
1495                 for (j = 0; j < l_height; ++j) {
1496                     for (k = 0; k < l_width; ++k) {
1497                         *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++));
1498                     }
1499                     l_src_ptr += l_stride;
1500                 }
1501             } else {
1502                 for (j = 0; j < l_height; ++j) {
1503                     for (k = 0; k < l_width; ++k) {
1504                         *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1505                     }
1506                     l_src_ptr += l_stride;
1507                 }
1508             }
1509
1510             p_dest = (OPJ_BYTE*) l_dest_ptr;
1511         }
1512         break;
1513         case 4: {
1514             OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1515             OPJ_INT32 * l_src_ptr = l_tilec->data;
1516
1517             for (j = 0; j < l_height; ++j) {
1518                 for (k = 0; k < l_width; ++k) {
1519                     *(l_dest_ptr++) = (*(l_src_ptr++));
1520                 }
1521                 l_src_ptr += l_stride;
1522             }
1523
1524             p_dest = (OPJ_BYTE*) l_dest_ptr;
1525         }
1526         break;
1527         }
1528
1529         ++l_img_comp;
1530         ++l_tilec;
1531     }
1532
1533     return OPJ_TRUE;
1534 }
1535
1536
1537
1538
1539 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1540 {
1541     OPJ_UINT32 compno, resno, bandno, precno;
1542     opj_tcd_tile_t *l_tile = 00;
1543     opj_tcd_tilecomp_t *l_tile_comp = 00;
1544     opj_tcd_resolution_t *l_res = 00;
1545     opj_tcd_band_t *l_band = 00;
1546     opj_tcd_precinct_t *l_precinct = 00;
1547     OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1548     void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1549
1550     if (! p_tcd) {
1551         return;
1552     }
1553
1554     if (! p_tcd->tcd_image) {
1555         return;
1556     }
1557
1558     if (p_tcd->m_is_decoder) {
1559         l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1560     } else {
1561         l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1562     }
1563
1564     l_tile = p_tcd->tcd_image->tiles;
1565     if (! l_tile) {
1566         return;
1567     }
1568
1569     l_tile_comp = l_tile->comps;
1570
1571     for (compno = 0; compno < l_tile->numcomps; ++compno) {
1572         l_res = l_tile_comp->resolutions;
1573         if (l_res) {
1574
1575             l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1576             for (resno = 0; resno < l_nb_resolutions; ++resno) {
1577                 l_band = l_res->bands;
1578                 for (bandno = 0; bandno < 3; ++bandno) {
1579                     l_precinct = l_band->precincts;
1580                     if (l_precinct) {
1581
1582                         l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1583                         for (precno = 0; precno < l_nb_precincts; ++precno) {
1584                             opj_tgt_destroy(l_precinct->incltree);
1585                             l_precinct->incltree = 00;
1586                             opj_tgt_destroy(l_precinct->imsbtree);
1587                             l_precinct->imsbtree = 00;
1588                             (*l_tcd_code_block_deallocate)(l_precinct);
1589                             ++l_precinct;
1590                         }
1591
1592                         opj_free(l_band->precincts);
1593                         l_band->precincts = 00;
1594                     }
1595                     ++l_band;
1596                 } /* for (resno */
1597                 ++l_res;
1598             }
1599
1600             opj_free(l_tile_comp->resolutions);
1601             l_tile_comp->resolutions = 00;
1602         }
1603
1604         if (l_tile_comp->ownsData && l_tile_comp->data) {
1605             opj_aligned_free(l_tile_comp->data);
1606             l_tile_comp->data = 00;
1607             l_tile_comp->ownsData = 0;
1608             l_tile_comp->data_size = 0;
1609             l_tile_comp->data_size_needed = 0;
1610         }
1611         ++l_tile_comp;
1612     }
1613
1614     opj_free(l_tile->comps);
1615     l_tile->comps = 00;
1616     opj_free(p_tcd->tcd_image->tiles);
1617     p_tcd->tcd_image->tiles = 00;
1618 }
1619
1620
1621 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1622                                   OPJ_BYTE * p_src_data,
1623                                   OPJ_UINT32 * p_data_read,
1624                                   OPJ_UINT32 p_max_src_size,
1625                                   opj_codestream_index_t *p_cstr_index,
1626                                   opj_event_mgr_t *p_manager
1627                                  )
1628 {
1629     opj_t2_t * l_t2;
1630
1631     l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1632     if (l_t2 == 00) {
1633         return OPJ_FALSE;
1634     }
1635
1636     if (! opj_t2_decode_packets(
1637                 l_t2,
1638                 p_tcd->tcd_tileno,
1639                 p_tcd->tcd_image->tiles,
1640                 p_src_data,
1641                 p_data_read,
1642                 p_max_src_size,
1643                 p_cstr_index,
1644                 p_manager)) {
1645         opj_t2_destroy(l_t2);
1646         return OPJ_FALSE;
1647     }
1648
1649     opj_t2_destroy(l_t2);
1650
1651     /*---------------CLEAN-------------------*/
1652     return OPJ_TRUE;
1653 }
1654
1655 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd)
1656 {
1657     OPJ_UINT32 compno;
1658     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1659     opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1660     opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1661     volatile OPJ_BOOL ret = OPJ_TRUE;
1662
1663     for (compno = 0; compno < l_tile->numcomps; ++compno) {
1664         opj_t1_decode_cblks(p_tcd->thread_pool, &ret, l_tile_comp, l_tccp);
1665         if (!ret) {
1666             break;
1667         }
1668         ++l_tile_comp;
1669         ++l_tccp;
1670     }
1671
1672     opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1673
1674     return ret;
1675 }
1676
1677
1678 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1679 {
1680     OPJ_UINT32 compno;
1681     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1682     opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1683     opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1684     opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1685
1686     for (compno = 0; compno < l_tile->numcomps; compno++) {
1687         /*
1688         if (tcd->cp->reduce != 0) {
1689                 tcd->image->comps[compno].resno_decoded =
1690                         tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1691                 if (tcd->image->comps[compno].resno_decoded < 0)
1692                 {
1693                         return false;
1694                 }
1695         }
1696         numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1697         if(numres2decode > 0){
1698         */
1699
1700         if (l_tccp->qmfbid == 1) {
1701             if (! opj_dwt_decode(p_tcd->thread_pool, l_tile_comp,
1702                                  l_img_comp->resno_decoded + 1)) {
1703                 return OPJ_FALSE;
1704             }
1705         } else {
1706             if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded + 1)) {
1707                 return OPJ_FALSE;
1708             }
1709         }
1710
1711         ++l_tile_comp;
1712         ++l_img_comp;
1713         ++l_tccp;
1714     }
1715
1716     return OPJ_TRUE;
1717 }
1718 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1719 {
1720     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1721     opj_tcp_t * l_tcp = p_tcd->tcp;
1722     opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1723     OPJ_UINT32 l_samples, i;
1724
1725     if (! l_tcp->mct) {
1726         return OPJ_TRUE;
1727     }
1728
1729     l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1730                              (l_tile_comp->y1 - l_tile_comp->y0));
1731
1732     if (l_tile->numcomps >= 3) {
1733         /* testcase 1336.pdf.asan.47.376 */
1734         if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 -
1735                 l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1736                 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 -
1737                         l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1738                 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 -
1739                         l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1740             opj_event_msg(p_manager, EVT_ERROR,
1741                           "Tiles don't all have the same dimension. Skip the MCT step.\n");
1742             return OPJ_FALSE;
1743         } else if (l_tcp->mct == 2) {
1744             OPJ_BYTE ** l_data;
1745
1746             if (! l_tcp->m_mct_decoding_matrix) {
1747                 return OPJ_TRUE;
1748             }
1749
1750             l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
1751             if (! l_data) {
1752                 return OPJ_FALSE;
1753             }
1754
1755             for (i = 0; i < l_tile->numcomps; ++i) {
1756                 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1757                 ++l_tile_comp;
1758             }
1759
1760             if (! opj_mct_decode_custom(/* MCT data */
1761                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1762                         /* size of components */
1763                         l_samples,
1764                         /* components */
1765                         l_data,
1766                         /* nb of components (i.e. size of pData) */
1767                         l_tile->numcomps,
1768                         /* tells if the data is signed */
1769                         p_tcd->image->comps->sgnd)) {
1770                 opj_free(l_data);
1771                 return OPJ_FALSE;
1772             }
1773
1774             opj_free(l_data);
1775         } else {
1776             if (l_tcp->tccps->qmfbid == 1) {
1777                 opj_mct_decode(l_tile->comps[0].data,
1778                                l_tile->comps[1].data,
1779                                l_tile->comps[2].data,
1780                                l_samples);
1781             } else {
1782                 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1783                                     (OPJ_FLOAT32*)l_tile->comps[1].data,
1784                                     (OPJ_FLOAT32*)l_tile->comps[2].data,
1785                                     l_samples);
1786             }
1787         }
1788     } else {
1789         opj_event_msg(p_manager, EVT_ERROR,
1790                       "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
1791                       l_tile->numcomps);
1792     }
1793
1794     return OPJ_TRUE;
1795 }
1796
1797
1798 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
1799 {
1800     OPJ_UINT32 compno;
1801     opj_tcd_tilecomp_t * l_tile_comp = 00;
1802     opj_tccp_t * l_tccp = 00;
1803     opj_image_comp_t * l_img_comp = 00;
1804     opj_tcd_resolution_t* l_res = 00;
1805     opj_tcd_tile_t * l_tile;
1806     OPJ_UINT32 l_width, l_height, i, j;
1807     OPJ_INT32 * l_current_ptr;
1808     OPJ_INT32 l_min, l_max;
1809     OPJ_UINT32 l_stride;
1810
1811     l_tile = p_tcd->tcd_image->tiles;
1812     l_tile_comp = l_tile->comps;
1813     l_tccp = p_tcd->tcp->tccps;
1814     l_img_comp = p_tcd->image->comps;
1815
1816     for (compno = 0; compno < l_tile->numcomps; compno++) {
1817         l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1818         l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1819         l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1820         l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1821
1822         assert(l_height == 0 ||
1823                l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1824
1825         if (l_img_comp->sgnd) {
1826             l_min = -(1 << (l_img_comp->prec - 1));
1827             l_max = (1 << (l_img_comp->prec - 1)) - 1;
1828         } else {
1829             l_min = 0;
1830             l_max = (1 << l_img_comp->prec) - 1;
1831         }
1832
1833         l_current_ptr = l_tile_comp->data;
1834
1835         if (l_tccp->qmfbid == 1) {
1836             for (j = 0; j < l_height; ++j) {
1837                 for (i = 0; i < l_width; ++i) {
1838                     *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
1839                                                    l_max);
1840                     ++l_current_ptr;
1841                 }
1842                 l_current_ptr += l_stride;
1843             }
1844         } else {
1845             for (j = 0; j < l_height; ++j) {
1846                 for (i = 0; i < l_width; ++i) {
1847                     OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1848                     *l_current_ptr = opj_int_clamp((OPJ_INT32)opj_lrintf(l_value) +
1849                                                    l_tccp->m_dc_level_shift, l_min, l_max); ;
1850                     ++l_current_ptr;
1851                 }
1852                 l_current_ptr += l_stride;
1853             }
1854         }
1855
1856         ++l_img_comp;
1857         ++l_tccp;
1858         ++l_tile_comp;
1859     }
1860
1861     return OPJ_TRUE;
1862 }
1863
1864
1865
1866 /**
1867  * Deallocates the encoding data of the given precinct.
1868  */
1869 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
1870 {
1871     OPJ_UINT32 cblkno, l_nb_code_blocks;
1872
1873     opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1874     if (l_code_block) {
1875         /*fprintf(stderr,"deallocate codeblock:{\n");*/
1876         /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/
1877         /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1878                         l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/
1879
1880
1881         l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1882         /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1883
1884         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1885
1886             if (l_code_block->data) {
1887                 opj_free(l_code_block->data);
1888                 l_code_block->data = 00;
1889             }
1890
1891             if (l_code_block->segs) {
1892                 opj_free(l_code_block->segs);
1893                 l_code_block->segs = 00;
1894             }
1895
1896             ++l_code_block;
1897         }
1898
1899         opj_free(p_precinct->cblks.dec);
1900         p_precinct->cblks.dec = 00;
1901     }
1902 }
1903
1904 /**
1905  * Deallocates the encoding data of the given precinct.
1906  */
1907 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
1908 {
1909     OPJ_UINT32 cblkno, l_nb_code_blocks;
1910
1911     opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1912     if (l_code_block) {
1913         l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1914
1915         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
1916             if (l_code_block->data) {
1917                 opj_free(l_code_block->data - 1);
1918                 l_code_block->data = 00;
1919             }
1920
1921             if (l_code_block->layers) {
1922                 opj_free(l_code_block->layers);
1923                 l_code_block->layers = 00;
1924             }
1925
1926             if (l_code_block->passes) {
1927                 opj_free(l_code_block->passes);
1928                 l_code_block->passes = 00;
1929             }
1930             ++l_code_block;
1931         }
1932
1933         opj_free(p_precinct->cblks.enc);
1934
1935         p_precinct->cblks.enc = 00;
1936     }
1937 }
1938
1939 OPJ_UINT32 opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
1940 {
1941     OPJ_UINT32 i, l_data_size = 0;
1942     opj_image_comp_t * l_img_comp = 00;
1943     opj_tcd_tilecomp_t * l_tilec = 00;
1944     OPJ_UINT32 l_size_comp, l_remaining;
1945
1946     l_tilec = p_tcd->tcd_image->tiles->comps;
1947     l_img_comp = p_tcd->image->comps;
1948     for (i = 0; i < p_tcd->image->numcomps; ++i) {
1949         l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1950         l_remaining = l_img_comp->prec & 7;  /* (%8) */
1951
1952         if (l_remaining) {
1953             ++l_size_comp;
1954         }
1955
1956         if (l_size_comp == 3) {
1957             l_size_comp = 4;
1958         }
1959
1960         l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) *
1961                        (l_tilec->y1 - l_tilec->y0));
1962         ++l_img_comp;
1963         ++l_tilec;
1964     }
1965
1966     return l_data_size;
1967 }
1968
1969 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
1970 {
1971     OPJ_UINT32 compno;
1972     opj_tcd_tilecomp_t * l_tile_comp = 00;
1973     opj_tccp_t * l_tccp = 00;
1974     opj_image_comp_t * l_img_comp = 00;
1975     opj_tcd_tile_t * l_tile;
1976     OPJ_UINT32 l_nb_elem, i;
1977     OPJ_INT32 * l_current_ptr;
1978
1979     l_tile = p_tcd->tcd_image->tiles;
1980     l_tile_comp = l_tile->comps;
1981     l_tccp = p_tcd->tcp->tccps;
1982     l_img_comp = p_tcd->image->comps;
1983
1984     for (compno = 0; compno < l_tile->numcomps; compno++) {
1985         l_current_ptr = l_tile_comp->data;
1986         l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
1987                                  (l_tile_comp->y1 - l_tile_comp->y0));
1988
1989         if (l_tccp->qmfbid == 1) {
1990             for (i = 0; i < l_nb_elem; ++i) {
1991                 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1992                 ++l_current_ptr;
1993             }
1994         } else {
1995             for (i = 0; i < l_nb_elem; ++i) {
1996                 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
1997                 ++l_current_ptr;
1998             }
1999         }
2000
2001         ++l_img_comp;
2002         ++l_tccp;
2003         ++l_tile_comp;
2004     }
2005
2006     return OPJ_TRUE;
2007 }
2008
2009 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2010 {
2011     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2012     opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2013     OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) *
2014                                       (l_tile_comp->y1 - l_tile_comp->y0));
2015     OPJ_UINT32 i;
2016     OPJ_BYTE ** l_data = 00;
2017     opj_tcp_t * l_tcp = p_tcd->tcp;
2018
2019     if (!p_tcd->tcp->mct) {
2020         return OPJ_TRUE;
2021     }
2022
2023     if (p_tcd->tcp->mct == 2) {
2024         if (! p_tcd->tcp->m_mct_coding_matrix) {
2025             return OPJ_TRUE;
2026         }
2027
2028         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2029         if (! l_data) {
2030             return OPJ_FALSE;
2031         }
2032
2033         for (i = 0; i < l_tile->numcomps; ++i) {
2034             l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2035             ++l_tile_comp;
2036         }
2037
2038         if (! opj_mct_encode_custom(/* MCT data */
2039                     (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2040                     /* size of components */
2041                     samples,
2042                     /* components */
2043                     l_data,
2044                     /* nb of components (i.e. size of pData) */
2045                     l_tile->numcomps,
2046                     /* tells if the data is signed */
2047                     p_tcd->image->comps->sgnd)) {
2048             opj_free(l_data);
2049             return OPJ_FALSE;
2050         }
2051
2052         opj_free(l_data);
2053     } else if (l_tcp->tccps->qmfbid == 0) {
2054         opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2055                             l_tile->comps[2].data, samples);
2056     } else {
2057         opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2058                        l_tile->comps[2].data, samples);
2059     }
2060
2061     return OPJ_TRUE;
2062 }
2063
2064 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2065 {
2066     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2067     opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2068     opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2069     OPJ_UINT32 compno;
2070
2071     for (compno = 0; compno < l_tile->numcomps; ++compno) {
2072         if (l_tccp->qmfbid == 1) {
2073             if (! opj_dwt_encode(l_tile_comp)) {
2074                 return OPJ_FALSE;
2075             }
2076         } else if (l_tccp->qmfbid == 0) {
2077             if (! opj_dwt_encode_real(l_tile_comp)) {
2078                 return OPJ_FALSE;
2079             }
2080         }
2081
2082         ++l_tile_comp;
2083         ++l_tccp;
2084     }
2085
2086     return OPJ_TRUE;
2087 }
2088
2089 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2090 {
2091     opj_t1_t * l_t1;
2092     const OPJ_FLOAT64 * l_mct_norms;
2093     OPJ_UINT32 l_mct_numcomps = 0U;
2094     opj_tcp_t * l_tcp = p_tcd->tcp;
2095
2096     l_t1 = opj_t1_create(OPJ_TRUE);
2097     if (l_t1 == 00) {
2098         return OPJ_FALSE;
2099     }
2100
2101     if (l_tcp->mct == 1) {
2102         l_mct_numcomps = 3U;
2103         /* irreversible encoding */
2104         if (l_tcp->tccps->qmfbid == 0) {
2105             l_mct_norms = opj_mct_get_mct_norms_real();
2106         } else {
2107             l_mct_norms = opj_mct_get_mct_norms();
2108         }
2109     } else {
2110         l_mct_numcomps = p_tcd->image->numcomps;
2111         l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2112     }
2113
2114     if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2115                               l_mct_numcomps)) {
2116         opj_t1_destroy(l_t1);
2117         return OPJ_FALSE;
2118     }
2119
2120     opj_t1_destroy(l_t1);
2121
2122     return OPJ_TRUE;
2123 }
2124
2125 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2126                                   OPJ_BYTE * p_dest_data,
2127                                   OPJ_UINT32 * p_data_written,
2128                                   OPJ_UINT32 p_max_dest_size,
2129                                   opj_codestream_info_t *p_cstr_info)
2130 {
2131     opj_t2_t * l_t2;
2132
2133     l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2134     if (l_t2 == 00) {
2135         return OPJ_FALSE;
2136     }
2137
2138     if (! opj_t2_encode_packets(
2139                 l_t2,
2140                 p_tcd->tcd_tileno,
2141                 p_tcd->tcd_image->tiles,
2142                 p_tcd->tcp->numlayers,
2143                 p_dest_data,
2144                 p_data_written,
2145                 p_max_dest_size,
2146                 p_cstr_info,
2147                 p_tcd->tp_num,
2148                 p_tcd->tp_pos,
2149                 p_tcd->cur_pino,
2150                 FINAL_PASS)) {
2151         opj_t2_destroy(l_t2);
2152         return OPJ_FALSE;
2153     }
2154
2155     opj_t2_destroy(l_t2);
2156
2157     /*---------------CLEAN-------------------*/
2158     return OPJ_TRUE;
2159 }
2160
2161
2162 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2163         OPJ_BYTE * p_dest_data,
2164         OPJ_UINT32 p_max_dest_size,
2165         opj_codestream_info_t *p_cstr_info)
2166 {
2167     opj_cp_t * l_cp = p_tcd->cp;
2168     OPJ_UINT32 l_nb_written = 0;
2169
2170     if (p_cstr_info)  {
2171         p_cstr_info->index_write = 0;
2172     }
2173
2174     if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2175             l_cp->m_specific_param.m_enc.m_fixed_quality)  {
2176         /* fixed_quality */
2177         /* Normal Rate/distortion allocation */
2178         if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2179                                    p_cstr_info)) {
2180             return OPJ_FALSE;
2181         }
2182     } else {
2183         /* Fixed layer allocation */
2184         opj_tcd_rateallocate_fixed(p_tcd);
2185     }
2186
2187     return OPJ_TRUE;
2188 }
2189
2190
2191 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2192                                 OPJ_BYTE * p_src,
2193                                 OPJ_UINT32 p_src_length)
2194 {
2195     OPJ_UINT32 i, j, l_data_size = 0;
2196     opj_image_comp_t * l_img_comp = 00;
2197     opj_tcd_tilecomp_t * l_tilec = 00;
2198     OPJ_UINT32 l_size_comp, l_remaining;
2199     OPJ_UINT32 l_nb_elem;
2200
2201     l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2202     if (l_data_size != p_src_length) {
2203         return OPJ_FALSE;
2204     }
2205
2206     l_tilec = p_tcd->tcd_image->tiles->comps;
2207     l_img_comp = p_tcd->image->comps;
2208     for (i = 0; i < p_tcd->image->numcomps; ++i) {
2209         l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2210         l_remaining = l_img_comp->prec & 7;  /* (%8) */
2211         l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 -
2212                                  l_tilec->y0));
2213
2214         if (l_remaining) {
2215             ++l_size_comp;
2216         }
2217
2218         if (l_size_comp == 3) {
2219             l_size_comp = 4;
2220         }
2221
2222         switch (l_size_comp) {
2223         case 1: {
2224             OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2225             OPJ_INT32 * l_dest_ptr = l_tilec->data;
2226
2227             if (l_img_comp->sgnd) {
2228                 for (j = 0; j < l_nb_elem; ++j) {
2229                     *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2230                 }
2231             } else {
2232                 for (j = 0; j < l_nb_elem; ++j) {
2233                     *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2234                 }
2235             }
2236
2237             p_src = (OPJ_BYTE*) l_src_ptr;
2238         }
2239         break;
2240         case 2: {
2241             OPJ_INT32 * l_dest_ptr = l_tilec->data;
2242             OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2243
2244             if (l_img_comp->sgnd) {
2245                 for (j = 0; j < l_nb_elem; ++j) {
2246                     *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2247                 }
2248             } else {
2249                 for (j = 0; j < l_nb_elem; ++j) {
2250                     *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2251                 }
2252             }
2253
2254             p_src = (OPJ_BYTE*) l_src_ptr;
2255         }
2256         break;
2257         case 4: {
2258             OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2259             OPJ_INT32 * l_dest_ptr = l_tilec->data;
2260
2261             for (j = 0; j < l_nb_elem; ++j) {
2262                 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2263             }
2264
2265             p_src = (OPJ_BYTE*) l_src_ptr;
2266         }
2267         break;
2268         }
2269
2270         ++l_img_comp;
2271         ++l_tilec;
2272     }
2273
2274     return OPJ_TRUE;
2275 }