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