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