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