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