• 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  * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
30  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include "opj_includes.h"
43 #include "opj_common.h"
44 
45 /* ----------------------------------------------------------------------- */
46 
47 /* TODO MSD: */
48 #ifdef TODO_MSD
tcd_dump(FILE * fd,opj_tcd_t * tcd,opj_tcd_image_t * img)49 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
50 {
51     int tileno, compno, resno, bandno, precno;/*, cblkno;*/
52 
53     fprintf(fd, "image {\n");
54     fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
55             img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
56             tcd->image->y1);
57 
58     for (tileno = 0; tileno < img->th * img->tw; tileno++) {
59         opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
60         fprintf(fd, "  tile {\n");
61         fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
62                 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
63         for (compno = 0; compno < tile->numcomps; compno++) {
64             opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
65             fprintf(fd, "    tilec {\n");
66             fprintf(fd,
67                     "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
68                     tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
69             for (resno = 0; resno < tilec->numresolutions; resno++) {
70                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
71                 fprintf(fd, "\n   res {\n");
72                 fprintf(fd,
73                         "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
74                         res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
75                 for (bandno = 0; bandno < res->numbands; bandno++) {
76                     opj_tcd_band_t *band = &res->bands[bandno];
77                     fprintf(fd, "        band {\n");
78                     fprintf(fd,
79                             "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
80                             band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
81                     for (precno = 0; precno < res->pw * res->ph; precno++) {
82                         opj_tcd_precinct_t *prec = &band->precincts[precno];
83                         fprintf(fd, "          prec {\n");
84                         fprintf(fd,
85                                 "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
86                                 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
87                         /*
88                         for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
89                                 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
90                                 fprintf(fd, "            cblk {\n");
91                                 fprintf(fd,
92                                         "              x0=%d, y0=%d, x1=%d, y1=%d\n",
93                                         cblk->x0, cblk->y0, cblk->x1, cblk->y1);
94                                 fprintf(fd, "            }\n");
95                         }
96                         */
97                         fprintf(fd, "          }\n");
98                     }
99                     fprintf(fd, "        }\n");
100                 }
101                 fprintf(fd, "      }\n");
102             }
103             fprintf(fd, "    }\n");
104         }
105         fprintf(fd, "  }\n");
106     }
107     fprintf(fd, "}\n");
108 }
109 #endif
110 
111 /**
112  * Initializes tile coding/decoding
113  */
114 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
115         OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
116         opj_event_mgr_t* manager);
117 
118 /**
119 * Allocates memory for a decoding code block.
120 */
121 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
122         p_code_block);
123 
124 /**
125  * Deallocates the decoding data of the given precinct.
126  */
127 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct);
128 
129 /**
130  * Allocates memory for an encoding code block (but not data).
131  */
132 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
133         p_code_block);
134 
135 /**
136  * Allocates data for an encoding code block
137  */
138 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
139         p_code_block);
140 
141 /**
142  * Deallocates the encoding data of the given precinct.
143  */
144 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct);
145 
146 
147 /**
148 Free the memory allocated for encoding
149 @param tcd TCD handle
150 */
151 static void opj_tcd_free_tile(opj_tcd_t *tcd);
152 
153 
154 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
155                                   OPJ_BYTE * p_src_data,
156                                   OPJ_UINT32 * p_data_read,
157                                   OPJ_UINT32 p_max_src_size,
158                                   opj_codestream_index_t *p_cstr_index,
159                                   opj_event_mgr_t *p_manager);
160 
161 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd,
162                                   opj_event_mgr_t *p_manager);
163 
164 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd);
165 
166 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd,
167                                    opj_event_mgr_t *p_manager);
168 
169 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd);
170 
171 
172 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd);
173 
174 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd);
175 
176 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd);
177 
178 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd);
179 
180 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
181                                   OPJ_BYTE * p_dest_data,
182                                   OPJ_UINT32 * p_data_written,
183                                   OPJ_UINT32 p_max_dest_size,
184                                   opj_codestream_info_t *p_cstr_info,
185                                   opj_event_mgr_t *p_manager);
186 
187 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
188         OPJ_BYTE * p_dest_data,
189         OPJ_UINT32 p_max_dest_size,
190         opj_codestream_info_t *p_cstr_info,
191         opj_event_mgr_t *p_manager);
192 
193 
194 static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *tcd,
195         OPJ_UINT32 compno);
196 
197 /* ----------------------------------------------------------------------- */
198 
199 /**
200 Create a new TCD handle
201 */
opj_tcd_create(OPJ_BOOL p_is_decoder)202 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
203 {
204     opj_tcd_t *l_tcd = 00;
205 
206     /* create the tcd structure */
207     l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
208     if (!l_tcd) {
209         return 00;
210     }
211 
212     l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
213 
214     l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
215     if (!l_tcd->tcd_image) {
216         opj_free(l_tcd);
217         return 00;
218     }
219 
220     return l_tcd;
221 }
222 
223 
224 /* ----------------------------------------------------------------------- */
225 
opj_tcd_rateallocate_fixed(opj_tcd_t * tcd)226 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
227 {
228     OPJ_UINT32 layno;
229 
230     for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
231         opj_tcd_makelayer_fixed(tcd, layno, 1);
232     }
233 }
234 
235 
opj_tcd_makelayer(opj_tcd_t * tcd,OPJ_UINT32 layno,OPJ_FLOAT64 thresh,OPJ_UINT32 final)236 void opj_tcd_makelayer(opj_tcd_t *tcd,
237                        OPJ_UINT32 layno,
238                        OPJ_FLOAT64 thresh,
239                        OPJ_UINT32 final)
240 {
241     OPJ_UINT32 compno, resno, bandno, precno, cblkno;
242     OPJ_UINT32 passno;
243 
244     opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
245 
246     tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
247 
248     for (compno = 0; compno < tcd_tile->numcomps; compno++) {
249         opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
250 
251         for (resno = 0; resno < tilec->numresolutions; resno++) {
252             opj_tcd_resolution_t *res = &tilec->resolutions[resno];
253 
254             for (bandno = 0; bandno < res->numbands; bandno++) {
255                 opj_tcd_band_t *band = &res->bands[bandno];
256 
257                 /* Skip empty bands */
258                 if (opj_tcd_is_band_empty(band)) {
259                     continue;
260                 }
261 
262                 for (precno = 0; precno < res->pw * res->ph; precno++) {
263                     opj_tcd_precinct_t *prc = &band->precincts[precno];
264 
265                     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
266                         opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
267                         opj_tcd_layer_t *layer = &cblk->layers[layno];
268                         OPJ_UINT32 n;
269 
270                         if (layno == 0) {
271                             cblk->numpassesinlayers = 0;
272                         }
273 
274                         n = cblk->numpassesinlayers;
275 
276                         if (thresh < 0) {
277                             /* Special value to indicate to use all passes */
278                             n = cblk->totalpasses;
279                         } else {
280                             for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
281                                 OPJ_UINT32 dr;
282                                 OPJ_FLOAT64 dd;
283                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
284 
285                                 if (n == 0) {
286                                     dr = pass->rate;
287                                     dd = pass->distortiondec;
288                                 } else {
289                                     dr = pass->rate - cblk->passes[n - 1].rate;
290                                     dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
291                                 }
292 
293                                 if (!dr) {
294                                     if (dd != 0) {
295                                         n = passno + 1;
296                                     }
297                                     continue;
298                                 }
299                                 if (thresh - (dd / dr) <
300                                         DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
301                                     n = passno + 1;
302                                 }
303                             }
304                         }
305 
306                         layer->numpasses = n - cblk->numpassesinlayers;
307 
308                         if (!layer->numpasses) {
309                             layer->disto = 0;
310                             continue;
311                         }
312 
313                         if (cblk->numpassesinlayers == 0) {
314                             layer->len = cblk->passes[n - 1].rate;
315                             layer->data = cblk->data;
316                             layer->disto = cblk->passes[n - 1].distortiondec;
317                         } else {
318                             layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
319                                          1].rate;
320                             layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
321                             layer->disto = cblk->passes[n - 1].distortiondec -
322                                            cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
323                         }
324 
325                         tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
326 
327                         if (final) {
328                             cblk->numpassesinlayers = n;
329                         }
330                     }
331                 }
332             }
333         }
334     }
335 }
336 
opj_tcd_makelayer_fixed(opj_tcd_t * tcd,OPJ_UINT32 layno,OPJ_UINT32 final)337 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
338                              OPJ_UINT32 final)
339 {
340     OPJ_UINT32 compno, resno, bandno, precno, cblkno;
341     OPJ_INT32 value;                        /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
342     OPJ_INT32 matrice[10][10][3];
343     OPJ_UINT32 i, j, k;
344 
345     opj_cp_t *cp = tcd->cp;
346     opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
347     opj_tcp_t *tcd_tcp = tcd->tcp;
348 
349     for (compno = 0; compno < tcd_tile->numcomps; compno++) {
350         opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
351 
352         for (i = 0; i < tcd_tcp->numlayers; i++) {
353             for (j = 0; j < tilec->numresolutions; j++) {
354                 for (k = 0; k < 3; k++) {
355                     matrice[i][j][k] =
356                         (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
357                                       tilec->numresolutions * 3 + j * 3 + k]
358                                     * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
359                 }
360             }
361         }
362 
363         for (resno = 0; resno < tilec->numresolutions; resno++) {
364             opj_tcd_resolution_t *res = &tilec->resolutions[resno];
365 
366             for (bandno = 0; bandno < res->numbands; bandno++) {
367                 opj_tcd_band_t *band = &res->bands[bandno];
368 
369                 /* Skip empty bands */
370                 if (opj_tcd_is_band_empty(band)) {
371                     continue;
372                 }
373 
374                 for (precno = 0; precno < res->pw * res->ph; precno++) {
375                     opj_tcd_precinct_t *prc = &band->precincts[precno];
376 
377                     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
378                         opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
379                         opj_tcd_layer_t *layer = &cblk->layers[layno];
380                         OPJ_UINT32 n;
381                         OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
382                                                      cblk->numbps); /* number of bit-plan equal to zero */
383 
384                         /* Correction of the matrix of coefficient to include the IMSB information */
385                         if (layno == 0) {
386                             value = matrice[layno][resno][bandno];
387                             if (imsb >= value) {
388                                 value = 0;
389                             } else {
390                                 value -= imsb;
391                             }
392                         } else {
393                             value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
394                             if (imsb >= matrice[layno - 1][resno][bandno]) {
395                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
396                                 if (value < 0) {
397                                     value = 0;
398                                 }
399                             }
400                         }
401 
402                         if (layno == 0) {
403                             cblk->numpassesinlayers = 0;
404                         }
405 
406                         n = cblk->numpassesinlayers;
407                         if (cblk->numpassesinlayers == 0) {
408                             if (value != 0) {
409                                 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
410                             } else {
411                                 n = cblk->numpassesinlayers;
412                             }
413                         } else {
414                             n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
415                         }
416 
417                         layer->numpasses = n - cblk->numpassesinlayers;
418 
419                         if (!layer->numpasses) {
420                             continue;
421                         }
422 
423                         if (cblk->numpassesinlayers == 0) {
424                             layer->len = cblk->passes[n - 1].rate;
425                             layer->data = cblk->data;
426                         } else {
427                             layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
428                                          1].rate;
429                             layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
430                         }
431 
432                         if (final) {
433                             cblk->numpassesinlayers = n;
434                         }
435                     }
436                 }
437             }
438         }
439     }
440 }
441 
opj_tcd_rateallocate(opj_tcd_t * tcd,OPJ_BYTE * dest,OPJ_UINT32 * p_data_written,OPJ_UINT32 len,opj_codestream_info_t * cstr_info,opj_event_mgr_t * p_manager)442 OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
443                               OPJ_BYTE *dest,
444                               OPJ_UINT32 * p_data_written,
445                               OPJ_UINT32 len,
446                               opj_codestream_info_t *cstr_info,
447                               opj_event_mgr_t *p_manager)
448 {
449     OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
450     OPJ_UINT32 passno;
451     OPJ_FLOAT64 min, max;
452     OPJ_FLOAT64 cumdisto[100];      /* fixed_quality */
453     const OPJ_FLOAT64 K = 1;                /* 1.1; fixed_quality */
454     OPJ_FLOAT64 maxSE = 0;
455 
456     opj_cp_t *cp = tcd->cp;
457     opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
458     opj_tcp_t *tcd_tcp = tcd->tcp;
459 
460     min = DBL_MAX;
461     max = 0;
462 
463     tcd_tile->numpix = 0;           /* fixed_quality */
464 
465     for (compno = 0; compno < tcd_tile->numcomps; compno++) {
466         opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
467         tilec->numpix = 0;
468 
469         for (resno = 0; resno < tilec->numresolutions; resno++) {
470             opj_tcd_resolution_t *res = &tilec->resolutions[resno];
471 
472             for (bandno = 0; bandno < res->numbands; bandno++) {
473                 opj_tcd_band_t *band = &res->bands[bandno];
474 
475                 /* Skip empty bands */
476                 if (opj_tcd_is_band_empty(band)) {
477                     continue;
478                 }
479 
480                 for (precno = 0; precno < res->pw * res->ph; precno++) {
481                     opj_tcd_precinct_t *prc = &band->precincts[precno];
482 
483                     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
484                         opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
485 
486                         for (passno = 0; passno < cblk->totalpasses; passno++) {
487                             opj_tcd_pass_t *pass = &cblk->passes[passno];
488                             OPJ_INT32 dr;
489                             OPJ_FLOAT64 dd, rdslope;
490 
491                             if (passno == 0) {
492                                 dr = (OPJ_INT32)pass->rate;
493                                 dd = pass->distortiondec;
494                             } else {
495                                 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
496                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
497                             }
498 
499                             if (dr == 0) {
500                                 continue;
501                             }
502 
503                             rdslope = dd / dr;
504                             if (rdslope < min) {
505                                 min = rdslope;
506                             }
507 
508                             if (rdslope > max) {
509                                 max = rdslope;
510                             }
511                         } /* passno */
512 
513                         /* fixed_quality */
514                         tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
515                         tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
516                     } /* cbklno */
517                 } /* precno */
518             } /* bandno */
519         } /* resno */
520 
521         maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
522                   * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
523                  * ((OPJ_FLOAT64)(tilec->numpix));
524     } /* compno */
525 
526     /* index file */
527     if (cstr_info) {
528         opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
529         tile_info->numpix = tcd_tile->numpix;
530         tile_info->distotile = tcd_tile->distotile;
531         tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
532                                 OPJ_FLOAT64));
533         if (!tile_info->thresh) {
534             /* FIXME event manager error callback */
535             return OPJ_FALSE;
536         }
537     }
538 
539     for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
540         OPJ_FLOAT64 lo = min;
541         OPJ_FLOAT64 hi = max;
542         OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
543                                 OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
544         OPJ_FLOAT64 goodthresh = 0;
545         OPJ_FLOAT64 stable_thresh = 0;
546         OPJ_UINT32 i;
547         OPJ_FLOAT64 distotarget;                /* fixed_quality */
548 
549         /* fixed_quality */
550         distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
551                                              tcd_tcp->distoratio[layno] / 10));
552 
553         /* Don't try to find an optimal threshold but rather take everything not included yet, if
554           -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
555           -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
556           ==> possible to have some lossy layers and the last layer for sure lossless */
557         if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
558                 (tcd_tcp->rates[layno] > 0.0f)) ||
559                 ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
560                  (tcd_tcp->distoratio[layno] > 0.0))) {
561             opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
562             OPJ_FLOAT64 thresh = 0;
563 
564             if (t2 == 00) {
565                 return OPJ_FALSE;
566             }
567 
568             for (i = 0; i < 128; ++i) {
569                 OPJ_FLOAT64 distoachieved = 0;  /* fixed_quality */
570 
571                 thresh = (lo + hi) / 2;
572 
573                 opj_tcd_makelayer(tcd, layno, thresh, 0);
574 
575                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* fixed_quality */
576                     if (OPJ_IS_CINEMA(cp->rsiz)) {
577                         if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
578                                                     p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
579                                                     THRESH_CALC, p_manager)) {
580 
581                             lo = thresh;
582                             continue;
583                         } else {
584                             distoachieved = layno == 0 ?
585                                             tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
586 
587                             if (distoachieved < distotarget) {
588                                 hi = thresh;
589                                 stable_thresh = thresh;
590                                 continue;
591                             } else {
592                                 lo = thresh;
593                             }
594                         }
595                     } else {
596                         distoachieved = (layno == 0) ?
597                                         tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
598 
599                         if (distoachieved < distotarget) {
600                             hi = thresh;
601                             stable_thresh = thresh;
602                             continue;
603                         }
604                         lo = thresh;
605                     }
606                 } else {
607                     if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
608                                                 p_data_written, maxlen, cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino,
609                                                 THRESH_CALC, p_manager)) {
610                         /* TODO: what to do with l ??? seek / tell ??? */
611                         /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
612                         lo = thresh;
613                         continue;
614                     }
615 
616                     hi = thresh;
617                     stable_thresh = thresh;
618                 }
619             }
620 
621             goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
622 
623             opj_t2_destroy(t2);
624         } else {
625             /* Special value to indicate to use all passes */
626             goodthresh = -1;
627         }
628 
629         if (cstr_info) { /* Threshold for Marcela Index */
630             cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
631         }
632 
633         opj_tcd_makelayer(tcd, layno, goodthresh, 1);
634 
635         /* fixed_quality */
636         cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
637                           (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
638     }
639 
640     return OPJ_TRUE;
641 }
642 
opj_tcd_init(opj_tcd_t * p_tcd,opj_image_t * p_image,opj_cp_t * p_cp,opj_thread_pool_t * p_tp)643 OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
644                       opj_image_t * p_image,
645                       opj_cp_t * p_cp,
646                       opj_thread_pool_t* p_tp)
647 {
648     p_tcd->image = p_image;
649     p_tcd->cp = p_cp;
650 
651     p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
652                               sizeof(opj_tcd_tile_t));
653     if (! p_tcd->tcd_image->tiles) {
654         return OPJ_FALSE;
655     }
656 
657     p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
658                                          p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
659     if (! p_tcd->tcd_image->tiles->comps) {
660         return OPJ_FALSE;
661     }
662 
663     p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
664     p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
665     p_tcd->thread_pool = p_tp;
666 
667     return OPJ_TRUE;
668 }
669 
670 /**
671 Destroy a previously created TCD handle
672 */
opj_tcd_destroy(opj_tcd_t * tcd)673 void opj_tcd_destroy(opj_tcd_t *tcd)
674 {
675     if (tcd) {
676         opj_tcd_free_tile(tcd);
677 
678         if (tcd->tcd_image) {
679             opj_free(tcd->tcd_image);
680             tcd->tcd_image = 00;
681         }
682 
683         opj_free(tcd->used_component);
684 
685         opj_free(tcd);
686     }
687 }
688 
opj_alloc_tile_component_data(opj_tcd_tilecomp_t * l_tilec)689 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
690 {
691     if ((l_tilec->data == 00) ||
692             ((l_tilec->data_size_needed > l_tilec->data_size) &&
693              (l_tilec->ownsData == OPJ_FALSE))) {
694         l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
695         if (!l_tilec->data && l_tilec->data_size_needed != 0) {
696             return OPJ_FALSE;
697         }
698         /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
699         l_tilec->data_size = l_tilec->data_size_needed;
700         l_tilec->ownsData = OPJ_TRUE;
701     } else if (l_tilec->data_size_needed > l_tilec->data_size) {
702         /* We don't need to keep old data */
703         opj_image_data_free(l_tilec->data);
704         l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
705         if (! l_tilec->data) {
706             l_tilec->data_size = 0;
707             l_tilec->data_size_needed = 0;
708             l_tilec->ownsData = OPJ_FALSE;
709             return OPJ_FALSE;
710         }
711         /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
712         l_tilec->data_size = l_tilec->data_size_needed;
713         l_tilec->ownsData = OPJ_TRUE;
714     }
715     return OPJ_TRUE;
716 }
717 
718 /* ----------------------------------------------------------------------- */
719 
opj_tcd_init_tile(opj_tcd_t * p_tcd,OPJ_UINT32 p_tile_no,OPJ_BOOL isEncoder,OPJ_FLOAT32 fraction,OPJ_SIZE_T sizeof_block,opj_event_mgr_t * manager)720 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
721         OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block,
722         opj_event_mgr_t* manager)
723 {
724     OPJ_UINT32(*l_gain_ptr)(OPJ_UINT32) = 00;
725     OPJ_UINT32 compno, resno, bandno, precno, cblkno;
726     opj_tcp_t * l_tcp = 00;
727     opj_cp_t * l_cp = 00;
728     opj_tcd_tile_t * l_tile = 00;
729     opj_tccp_t *l_tccp = 00;
730     opj_tcd_tilecomp_t *l_tilec = 00;
731     opj_image_comp_t * l_image_comp = 00;
732     opj_tcd_resolution_t *l_res = 00;
733     opj_tcd_band_t *l_band = 00;
734     opj_stepsize_t * l_step_size = 00;
735     opj_tcd_precinct_t *l_current_precinct = 00;
736     opj_image_t *l_image = 00;
737     OPJ_UINT32 p, q;
738     OPJ_UINT32 l_level_no;
739     OPJ_UINT32 l_pdx, l_pdy;
740     OPJ_UINT32 l_gain;
741     OPJ_INT32 l_x0b, l_y0b;
742     OPJ_UINT32 l_tx0, l_ty0;
743     /* extent of precincts , top left, bottom right**/
744     OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
745     /* number of precinct for a resolution */
746     OPJ_UINT32 l_nb_precincts;
747     /* room needed to store l_nb_precinct precinct for a resolution */
748     OPJ_UINT32 l_nb_precinct_size;
749     /* number of code blocks for a precinct*/
750     OPJ_UINT32 l_nb_code_blocks;
751     /* room needed to store l_nb_code_blocks code blocks for a precinct*/
752     OPJ_UINT32 l_nb_code_blocks_size;
753     /* size of data for a tile */
754     OPJ_UINT32 l_data_size;
755 
756     l_cp = p_tcd->cp;
757     l_tcp = &(l_cp->tcps[p_tile_no]);
758     l_tile = p_tcd->tcd_image->tiles;
759     l_tccp = l_tcp->tccps;
760     l_tilec = l_tile->comps;
761     l_image = p_tcd->image;
762     l_image_comp = p_tcd->image->comps;
763 
764     p = p_tile_no % l_cp->tw;       /* tile coordinates */
765     q = p_tile_no / l_cp->tw;
766     /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
767 
768     /* 4 borders of the tile rescale on the image if necessary */
769     l_tx0 = l_cp->tx0 + p *
770             l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
771     l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
772     l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
773                                          l_image->x1);
774     /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
775     if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
776         opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
777         return OPJ_FALSE;
778     }
779     l_ty0 = l_cp->ty0 + q *
780             l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
781     l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
782     l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
783                                          l_image->y1);
784     /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
785     if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
786         opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
787         return OPJ_FALSE;
788     }
789 
790 
791     /* testcase 1888.pdf.asan.35.988 */
792     if (l_tccp->numresolutions == 0) {
793         opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
794         return OPJ_FALSE;
795     }
796     /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
797 
798     /*tile->numcomps = image->numcomps; */
799     for (compno = 0; compno < l_tile->numcomps; ++compno) {
800         /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
801         l_image_comp->resno_decoded = 0;
802         /* border of each l_tile component (global) */
803         l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
804         l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
805         l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
806         l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
807         l_tilec->compno = compno;
808         /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
809 
810         l_tilec->numresolutions = l_tccp->numresolutions;
811         if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
812             l_tilec->minimum_num_resolutions = 1;
813         } else {
814             l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
815                                                l_cp->m_specific_param.m_dec.m_reduce;
816         }
817 
818         if (isEncoder) {
819             OPJ_SIZE_T l_tile_data_size;
820 
821             if (l_tilec->x0 >= l_tilec->x1 || l_tilec->y0 >= l_tilec->y1) {
822                 opj_event_msg(manager, EVT_ERROR, "Invalid tile data\n");
823                 return OPJ_FALSE;
824             }
825 
826             /* compute l_data_size with overflow check */
827             OPJ_SIZE_T w = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0);
828             OPJ_SIZE_T h = (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
829 
830             /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
831             if (h > 0 && w > SIZE_MAX / h) {
832                 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
833                 return OPJ_FALSE;
834             }
835             l_tile_data_size = w * h;
836 
837             if (SIZE_MAX / sizeof(OPJ_UINT32) < l_tile_data_size) {
838                 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
839                 return OPJ_FALSE;
840             }
841             l_tile_data_size = l_tile_data_size * sizeof(OPJ_UINT32);
842 
843             l_tilec->data_size_needed = l_tile_data_size;
844         }
845 
846         l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
847                           opj_tcd_resolution_t);
848 
849         opj_image_data_free(l_tilec->data_win);
850         l_tilec->data_win = NULL;
851         l_tilec->win_x0 = 0;
852         l_tilec->win_y0 = 0;
853         l_tilec->win_x1 = 0;
854         l_tilec->win_y1 = 0;
855 
856         if (l_tilec->resolutions == 00) {
857             l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
858             if (! l_tilec->resolutions) {
859                 return OPJ_FALSE;
860             }
861             /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
862             l_tilec->resolutions_size = l_data_size;
863             memset(l_tilec->resolutions, 0, l_data_size);
864         } else if (l_data_size > l_tilec->resolutions_size) {
865             opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
866                     l_tilec->resolutions, l_data_size);
867             if (! new_resolutions) {
868                 opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
869                 opj_free(l_tilec->resolutions);
870                 l_tilec->resolutions = NULL;
871                 l_tilec->resolutions_size = 0;
872                 return OPJ_FALSE;
873             }
874             l_tilec->resolutions = new_resolutions;
875             /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
876             memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
877                    l_data_size - l_tilec->resolutions_size);
878             l_tilec->resolutions_size = l_data_size;
879         }
880 
881         l_level_no = l_tilec->numresolutions;
882         l_res = l_tilec->resolutions;
883         l_step_size = l_tccp->stepsizes;
884         if (l_tccp->qmfbid == 0) {
885             l_gain_ptr = &opj_dwt_getgain_real;
886         } else {
887             l_gain_ptr  = &opj_dwt_getgain;
888         }
889         /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
890 
891         for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
892             /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
893             OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
894             OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
895             OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
896 
897             --l_level_no;
898 
899             /* border for each resolution level (global) */
900             l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
901             l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
902             l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
903             l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
904 
905             /*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);*/
906             /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
907             l_pdx = l_tccp->prcw[resno];
908             l_pdy = l_tccp->prch[resno];
909             /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
910             /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
911             l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
912             l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
913             l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
914             l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
915             /*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 );*/
916 
917             l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
918                             l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
919             l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
920                             l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
921             /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
922 
923             if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
924                 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
925                 return OPJ_FALSE;
926             }
927             l_nb_precincts = l_res->pw * l_res->ph;
928 
929             if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
930                     l_nb_precincts) {
931                 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
932                 return OPJ_FALSE;
933             }
934             l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
935 
936             if (resno == 0) {
937                 tlcbgxstart = l_tl_prc_x_start;
938                 tlcbgystart = l_tl_prc_y_start;
939                 /*brcbgxend = l_br_prc_x_end;*/
940                 /* brcbgyend = l_br_prc_y_end;*/
941                 cbgwidthexpn = l_pdx;
942                 cbgheightexpn = l_pdy;
943                 l_res->numbands = 1;
944             } else {
945                 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
946                 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
947                 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
948                 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
949                 cbgwidthexpn = l_pdx - 1;
950                 cbgheightexpn = l_pdy - 1;
951                 l_res->numbands = 3;
952             }
953 
954             cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
955             cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
956             l_band = l_res->bands;
957 
958             for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
959                 OPJ_INT32 numbps;
960                 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
961 
962                 if (resno == 0) {
963                     l_band->bandno = 0 ;
964                     l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
965                     l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
966                     l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
967                     l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
968                 } else {
969                     l_band->bandno = bandno + 1;
970                     /* x0b = 1 if bandno = 1 or 3 */
971                     l_x0b = l_band->bandno & 1;
972                     /* y0b = 1 if bandno = 2 or 3 */
973                     l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
974                     /* l_band border (global) */
975                     l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
976                                                        l_level_no), (OPJ_INT32)(l_level_no + 1));
977                     l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
978                                                        l_level_no), (OPJ_INT32)(l_level_no + 1));
979                     l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
980                                                        l_level_no), (OPJ_INT32)(l_level_no + 1));
981                     l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
982                                                        l_level_no), (OPJ_INT32)(l_level_no + 1));
983                 }
984 
985                 if (isEncoder) {
986                     /* Skip empty bands */
987                     if (opj_tcd_is_band_empty(l_band)) {
988                         /* Do not zero l_band->precints to avoid leaks */
989                         /* but make sure we don't use it later, since */
990                         /* it will point to precincts of previous bands... */
991                         continue;
992                     }
993                 }
994 
995                 /** avoid an if with storing function pointer */
996                 l_gain = (*l_gain_ptr)(l_band->bandno);
997                 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
998                 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
999                                                   (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
1000                 /* Mb value of Equation E-2 in "E.1 Inverse quantization
1001                  * procedure" of the standard */
1002                 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
1003                                  1;
1004 
1005                 if (!l_band->precincts && (l_nb_precincts > 0U)) {
1006                     l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
1007                                             l_nb_precinct_size);
1008                     if (! l_band->precincts) {
1009                         opj_event_msg(manager, EVT_ERROR,
1010                                       "Not enough memory to handle band precints\n");
1011                         return OPJ_FALSE;
1012                     }
1013                     /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size);     */
1014                     memset(l_band->precincts, 0, l_nb_precinct_size);
1015                     l_band->precincts_data_size = l_nb_precinct_size;
1016                 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
1017 
1018                     opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
1019                             l_band->precincts,/*3 * */ l_nb_precinct_size);
1020                     if (! new_precincts) {
1021                         opj_event_msg(manager, EVT_ERROR,
1022                                       "Not enough memory to handle band precints\n");
1023                         opj_free(l_band->precincts);
1024                         l_band->precincts = NULL;
1025                         l_band->precincts_data_size = 0;
1026                         return OPJ_FALSE;
1027                     }
1028                     l_band->precincts = new_precincts;
1029                     /*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);*/
1030                     memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
1031                            l_nb_precinct_size - l_band->precincts_data_size);
1032                     l_band->precincts_data_size = l_nb_precinct_size;
1033                 }
1034 
1035                 l_current_precinct = l_band->precincts;
1036                 for (precno = 0; precno < l_nb_precincts; ++precno) {
1037                     OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1038                     OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1039                                           (1 << cbgwidthexpn);
1040                     OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1041                                           (1 << cbgheightexpn);
1042                     OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1043                     OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1044                     /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1045                     /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1046 
1047                     /* precinct size (global) */
1048                     /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1049 
1050                     l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1051                     l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1052                     l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1053                     l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1054                     /*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);*/
1055 
1056                     tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1057                                                         (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1058                     /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1059                     tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1060                                                         (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1061                     /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1062                     brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1063                                                      (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1064                     /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1065                     brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1066                                                      (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1067                     /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1068                     l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1069                                                           cblkwidthexpn);
1070                     l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1071                                                           cblkheightexpn);
1072                     if (l_current_precinct->cw && ((OPJ_UINT32)-1) / l_current_precinct->cw < l_current_precinct->ch) {
1073                         return OPJ_FALSE;
1074                     }
1075                     l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1076                     /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */
1077 
1078                     if (((OPJ_UINT32)-1) / (OPJ_UINT32)sizeof_block < l_nb_code_blocks) {
1079                         return OPJ_FALSE;
1080                     }
1081                     l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1082 
1083                     if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1084                         l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1085                         if (! l_current_precinct->cblks.blocks) {
1086                             return OPJ_FALSE;
1087                         }
1088                         /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1089 
1090                         memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1091 
1092                         l_current_precinct->block_size = l_nb_code_blocks_size;
1093                     } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1094                         void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1095                                                        l_nb_code_blocks_size);
1096                         if (! new_blocks) {
1097                             opj_free(l_current_precinct->cblks.blocks);
1098                             l_current_precinct->cblks.blocks = NULL;
1099                             l_current_precinct->block_size = 0;
1100                             opj_event_msg(manager, EVT_ERROR,
1101                                           "Not enough memory for current precinct codeblock element\n");
1102                             return OPJ_FALSE;
1103                         }
1104                         l_current_precinct->cblks.blocks = new_blocks;
1105                         /*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);     */
1106 
1107                         memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1108                                l_current_precinct->block_size
1109                                , 0
1110                                , l_nb_code_blocks_size - l_current_precinct->block_size);
1111 
1112                         l_current_precinct->block_size = l_nb_code_blocks_size;
1113                     }
1114 
1115                     if (! l_current_precinct->incltree) {
1116                         l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1117                                                        l_current_precinct->ch, manager);
1118                     } else {
1119                         l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1120                                                        l_current_precinct->cw, l_current_precinct->ch, manager);
1121                     }
1122 
1123                     if (! l_current_precinct->imsbtree) {
1124                         l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1125                                                        l_current_precinct->ch, manager);
1126                     } else {
1127                         l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1128                                                        l_current_precinct->cw, l_current_precinct->ch, manager);
1129                     }
1130 
1131                     for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1132                         OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1133                                                l_current_precinct->cw) * (1 << cblkwidthexpn);
1134                         OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1135                                                l_current_precinct->cw) * (1 << cblkheightexpn);
1136                         OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1137                         OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1138 
1139                         if (isEncoder) {
1140                             opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1141 
1142                             if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1143                                 return OPJ_FALSE;
1144                             }
1145                             /* code-block size (global) */
1146                             l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1147                             l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1148                             l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1149                             l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1150 
1151                             if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1152                                 return OPJ_FALSE;
1153                             }
1154                         } else {
1155                             opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1156 
1157                             if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1158                                 return OPJ_FALSE;
1159                             }
1160                             /* code-block size (global) */
1161                             l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1162                             l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1163                             l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1164                             l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1165                         }
1166                     }
1167                     ++l_current_precinct;
1168                 } /* precno */
1169             } /* bandno */
1170             ++l_res;
1171         } /* resno */
1172         ++l_tccp;
1173         ++l_tilec;
1174         ++l_image_comp;
1175     } /* compno */
1176     return OPJ_TRUE;
1177 }
1178 
opj_tcd_init_encode_tile(opj_tcd_t * p_tcd,OPJ_UINT32 p_tile_no,opj_event_mgr_t * p_manager)1179 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1180                                   opj_event_mgr_t* p_manager)
1181 {
1182     return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1183                              sizeof(opj_tcd_cblk_enc_t), p_manager);
1184 }
1185 
opj_tcd_init_decode_tile(opj_tcd_t * p_tcd,OPJ_UINT32 p_tile_no,opj_event_mgr_t * p_manager)1186 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1187                                   opj_event_mgr_t* p_manager)
1188 {
1189     return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1190                              sizeof(opj_tcd_cblk_dec_t), p_manager);
1191 }
1192 
1193 /**
1194  * Allocates memory for an encoding code block (but not data memory).
1195  */
opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t * p_code_block)1196 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1197         p_code_block)
1198 {
1199     if (! p_code_block->layers) {
1200         /* no memset since data */
1201         p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1202                                sizeof(opj_tcd_layer_t));
1203         if (! p_code_block->layers) {
1204             return OPJ_FALSE;
1205         }
1206     }
1207     if (! p_code_block->passes) {
1208         p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1209                                sizeof(opj_tcd_pass_t));
1210         if (! p_code_block->passes) {
1211             return OPJ_FALSE;
1212         }
1213     }
1214     return OPJ_TRUE;
1215 }
1216 
1217 /**
1218  * Allocates data memory for an encoding code block.
1219  */
opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t * p_code_block)1220 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1221         p_code_block)
1222 {
1223     OPJ_UINT32 l_data_size;
1224 
1225     /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1226     /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
1227     /* TODO: is there a theoretical upper-bound for the compressed code */
1228     /* block size ? */
1229     l_data_size = 2 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1230                                    (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1231 
1232     if (l_data_size > p_code_block->data_size) {
1233         if (p_code_block->data) {
1234             /* We refer to data - 1 since below we incremented it */
1235             opj_free(p_code_block->data - 1);
1236         }
1237         p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1238         if (! p_code_block->data) {
1239             p_code_block->data_size = 0U;
1240             return OPJ_FALSE;
1241         }
1242         p_code_block->data_size = l_data_size;
1243 
1244         /* We reserve the initial byte as a fake byte to a non-FF value */
1245         /* and increment the data pointer, so that opj_mqc_init_enc() */
1246         /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1247         /* it. */
1248         p_code_block->data[0] = 0;
1249         p_code_block->data += 1; /*why +1 ?*/
1250     }
1251     return OPJ_TRUE;
1252 }
1253 
1254 
opj_tcd_reinit_segment(opj_tcd_seg_t * seg)1255 void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1256 {
1257     memset(seg, 0, sizeof(opj_tcd_seg_t));
1258 }
1259 
1260 /**
1261  * Allocates memory for a decoding code block.
1262  */
opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t * p_code_block)1263 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1264         p_code_block)
1265 {
1266     if (! p_code_block->segs) {
1267 
1268         p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1269                              sizeof(opj_tcd_seg_t));
1270         if (! p_code_block->segs) {
1271             return OPJ_FALSE;
1272         }
1273         /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1274 
1275         p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1276         /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1277     } else {
1278         /* sanitize */
1279         opj_tcd_seg_t * l_segs = p_code_block->segs;
1280         OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1281         opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
1282         OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
1283         OPJ_UINT32 i;
1284 
1285         opj_aligned_free(p_code_block->decoded_data);
1286         p_code_block->decoded_data = 00;
1287 
1288         memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1289         p_code_block->segs = l_segs;
1290         p_code_block->m_current_max_segs = l_current_max_segs;
1291         for (i = 0; i < l_current_max_segs; ++i) {
1292             opj_tcd_reinit_segment(&l_segs[i]);
1293         }
1294         p_code_block->chunks = l_chunks;
1295         p_code_block->numchunksalloc = l_numchunksalloc;
1296     }
1297 
1298     return OPJ_TRUE;
1299 }
1300 
opj_tcd_get_decoded_tile_size(opj_tcd_t * p_tcd,OPJ_BOOL take_into_account_partial_decoding)1301 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd,
1302         OPJ_BOOL take_into_account_partial_decoding)
1303 {
1304     OPJ_UINT32 i;
1305     OPJ_UINT32 l_data_size = 0;
1306     opj_image_comp_t * l_img_comp = 00;
1307     opj_tcd_tilecomp_t * l_tile_comp = 00;
1308     opj_tcd_resolution_t * l_res = 00;
1309     OPJ_UINT32 l_size_comp, l_remaining;
1310     OPJ_UINT32 l_temp;
1311 
1312     l_tile_comp = p_tcd->tcd_image->tiles->comps;
1313     l_img_comp = p_tcd->image->comps;
1314 
1315     for (i = 0; i < p_tcd->image->numcomps; ++i) {
1316         OPJ_UINT32 w, h;
1317         l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1318         l_remaining = l_img_comp->prec & 7;  /* (%8) */
1319 
1320         if (l_remaining) {
1321             ++l_size_comp;
1322         }
1323 
1324         if (l_size_comp == 3) {
1325             l_size_comp = 4;
1326         }
1327 
1328         l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1329         if (take_into_account_partial_decoding && !p_tcd->whole_tile_decoding) {
1330             w = l_res->win_x1 - l_res->win_x0;
1331             h = l_res->win_y1 - l_res->win_y0;
1332         } else {
1333             w = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1334             h = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1335         }
1336         if (h > 0 && UINT_MAX / w < h) {
1337             return UINT_MAX;
1338         }
1339         l_temp = w * h;
1340         if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1341             return UINT_MAX;
1342         }
1343         l_temp *= l_size_comp;
1344 
1345         if (l_temp > UINT_MAX - l_data_size) {
1346             return UINT_MAX;
1347         }
1348         l_data_size += l_temp;
1349         ++l_img_comp;
1350         ++l_tile_comp;
1351     }
1352 
1353     return l_data_size;
1354 }
1355 
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,opj_event_mgr_t * p_manager)1356 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1357                              OPJ_UINT32 p_tile_no,
1358                              OPJ_BYTE *p_dest,
1359                              OPJ_UINT32 * p_data_written,
1360                              OPJ_UINT32 p_max_length,
1361                              opj_codestream_info_t *p_cstr_info,
1362                              opj_event_mgr_t *p_manager)
1363 {
1364 
1365     if (p_tcd->cur_tp_num == 0) {
1366 
1367         p_tcd->tcd_tileno = p_tile_no;
1368         p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1369 
1370         /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1371         if (p_cstr_info)  {
1372             OPJ_UINT32 l_num_packs = 0;
1373             OPJ_UINT32 i;
1374             opj_tcd_tilecomp_t *l_tilec_idx =
1375                 &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1376             opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1377 
1378             for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1379                 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1380 
1381                 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1382                 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1383 
1384                 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1385                 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1386                 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1387             }
1388             p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1389                     OPJ_SIZE_T)p_cstr_info->numcomps * (OPJ_SIZE_T)p_cstr_info->numlayers *
1390                                                   l_num_packs,
1391                                                   sizeof(opj_packet_info_t));
1392             if (!p_cstr_info->tile[p_tile_no].packet) {
1393                 /* FIXME event manager error callback */
1394                 return OPJ_FALSE;
1395             }
1396         }
1397         /* << INDEX */
1398 
1399         /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1400         /*---------------TILE-------------------*/
1401         if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1402             return OPJ_FALSE;
1403         }
1404         /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1405 
1406         /* FIXME _ProfStart(PGROUP_MCT); */
1407         if (! opj_tcd_mct_encode(p_tcd)) {
1408             return OPJ_FALSE;
1409         }
1410         /* FIXME _ProfStop(PGROUP_MCT); */
1411 
1412         /* FIXME _ProfStart(PGROUP_DWT); */
1413         if (! opj_tcd_dwt_encode(p_tcd)) {
1414             return OPJ_FALSE;
1415         }
1416         /* FIXME  _ProfStop(PGROUP_DWT); */
1417 
1418         /* FIXME  _ProfStart(PGROUP_T1); */
1419         if (! opj_tcd_t1_encode(p_tcd)) {
1420             return OPJ_FALSE;
1421         }
1422         /* FIXME _ProfStop(PGROUP_T1); */
1423 
1424         /* FIXME _ProfStart(PGROUP_RATE); */
1425         if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
1426                                            p_cstr_info, p_manager)) {
1427             return OPJ_FALSE;
1428         }
1429         /* FIXME _ProfStop(PGROUP_RATE); */
1430 
1431     }
1432     /*--------------TIER2------------------*/
1433 
1434     /* INDEX */
1435     if (p_cstr_info) {
1436         p_cstr_info->index_write = 1;
1437     }
1438     /* FIXME _ProfStart(PGROUP_T2); */
1439 
1440     if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1441                             p_cstr_info, p_manager)) {
1442         return OPJ_FALSE;
1443     }
1444     /* FIXME _ProfStop(PGROUP_T2); */
1445 
1446     /*---------------CLEAN-------------------*/
1447 
1448     return OPJ_TRUE;
1449 }
1450 
opj_tcd_decode_tile(opj_tcd_t * p_tcd,OPJ_UINT32 win_x0,OPJ_UINT32 win_y0,OPJ_UINT32 win_x1,OPJ_UINT32 win_y1,OPJ_UINT32 numcomps_to_decode,const OPJ_UINT32 * comps_indices,OPJ_BYTE * p_src,OPJ_UINT32 p_max_length,OPJ_UINT32 p_tile_no,opj_codestream_index_t * p_cstr_index,opj_event_mgr_t * p_manager)1451 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1452                              OPJ_UINT32 win_x0,
1453                              OPJ_UINT32 win_y0,
1454                              OPJ_UINT32 win_x1,
1455                              OPJ_UINT32 win_y1,
1456                              OPJ_UINT32 numcomps_to_decode,
1457                              const OPJ_UINT32 *comps_indices,
1458                              OPJ_BYTE *p_src,
1459                              OPJ_UINT32 p_max_length,
1460                              OPJ_UINT32 p_tile_no,
1461                              opj_codestream_index_t *p_cstr_index,
1462                              opj_event_mgr_t *p_manager
1463                             )
1464 {
1465     OPJ_UINT32 l_data_read;
1466     OPJ_UINT32 compno;
1467 
1468     p_tcd->tcd_tileno = p_tile_no;
1469     p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1470     p_tcd->win_x0 = win_x0;
1471     p_tcd->win_y0 = win_y0;
1472     p_tcd->win_x1 = win_x1;
1473     p_tcd->win_y1 = win_y1;
1474     p_tcd->whole_tile_decoding = OPJ_TRUE;
1475 
1476     opj_free(p_tcd->used_component);
1477     p_tcd->used_component = NULL;
1478 
1479     if (numcomps_to_decode) {
1480         OPJ_BOOL* used_component = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
1481                                    p_tcd->image->numcomps);
1482         if (used_component == NULL) {
1483             return OPJ_FALSE;
1484         }
1485         for (compno = 0; compno < numcomps_to_decode; compno++) {
1486             used_component[ comps_indices[compno] ] = OPJ_TRUE;
1487         }
1488 
1489         p_tcd->used_component = used_component;
1490     }
1491 
1492     for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1493         if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1494             continue;
1495         }
1496 
1497         if (!opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno)) {
1498             p_tcd->whole_tile_decoding = OPJ_FALSE;
1499             break;
1500         }
1501     }
1502 
1503     if (p_tcd->whole_tile_decoding) {
1504         for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1505             opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1506             opj_tcd_resolution_t *l_res = &
1507                                           (tilec->resolutions[tilec->minimum_num_resolutions - 1]);
1508             OPJ_SIZE_T l_data_size;
1509 
1510             /* compute l_data_size with overflow check */
1511             OPJ_SIZE_T res_w = (OPJ_SIZE_T)(l_res->x1 - l_res->x0);
1512             OPJ_SIZE_T res_h = (OPJ_SIZE_T)(l_res->y1 - l_res->y0);
1513 
1514             if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1515                 continue;
1516             }
1517 
1518             /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
1519             if (res_h > 0 && res_w > SIZE_MAX / res_h) {
1520                 opj_event_msg(p_manager, EVT_ERROR,
1521                               "Size of tile data exceeds system limits\n");
1522                 return OPJ_FALSE;
1523             }
1524             l_data_size = res_w * res_h;
1525 
1526             if (SIZE_MAX / sizeof(OPJ_UINT32) < l_data_size) {
1527                 opj_event_msg(p_manager, EVT_ERROR,
1528                               "Size of tile data exceeds system limits\n");
1529                 return OPJ_FALSE;
1530             }
1531             l_data_size *= sizeof(OPJ_UINT32);
1532 
1533             tilec->data_size_needed = l_data_size;
1534 
1535             if (!opj_alloc_tile_component_data(tilec)) {
1536                 opj_event_msg(p_manager, EVT_ERROR,
1537                               "Size of tile data exceeds system limits\n");
1538                 return OPJ_FALSE;
1539             }
1540         }
1541     } else {
1542         /* Compute restricted tile-component and tile-resolution coordinates */
1543         /* of the window of interest, but defer the memory allocation until */
1544         /* we know the resno_decoded */
1545         for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1546             OPJ_UINT32 resno;
1547             opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1548             opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1549 
1550             if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1551                 continue;
1552             }
1553 
1554             /* Compute the intersection of the area of interest, expressed in tile coordinates */
1555             /* with the tile coordinates */
1556             tilec->win_x0 = opj_uint_max(
1557                                 (OPJ_UINT32)tilec->x0,
1558                                 opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
1559             tilec->win_y0 = opj_uint_max(
1560                                 (OPJ_UINT32)tilec->y0,
1561                                 opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
1562             tilec->win_x1 = opj_uint_min(
1563                                 (OPJ_UINT32)tilec->x1,
1564                                 opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
1565             tilec->win_y1 = opj_uint_min(
1566                                 (OPJ_UINT32)tilec->y1,
1567                                 opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
1568             if (tilec->win_x1 < tilec->win_x0 ||
1569                     tilec->win_y1 < tilec->win_y0) {
1570                 /* We should not normally go there. The circumstance is when */
1571                 /* the tile coordinates do not intersect the area of interest */
1572                 /* Upper level logic should not even try to decode that tile */
1573                 opj_event_msg(p_manager, EVT_ERROR,
1574                               "Invalid tilec->win_xxx values\n");
1575                 return OPJ_FALSE;
1576             }
1577 
1578             for (resno = 0; resno < tilec->numresolutions; ++resno) {
1579                 opj_tcd_resolution_t *res = tilec->resolutions + resno;
1580                 res->win_x0 = opj_uint_ceildivpow2(tilec->win_x0,
1581                                                    tilec->numresolutions - 1 - resno);
1582                 res->win_y0 = opj_uint_ceildivpow2(tilec->win_y0,
1583                                                    tilec->numresolutions - 1 - resno);
1584                 res->win_x1 = opj_uint_ceildivpow2(tilec->win_x1,
1585                                                    tilec->numresolutions - 1 - resno);
1586                 res->win_y1 = opj_uint_ceildivpow2(tilec->win_y1,
1587                                                    tilec->numresolutions - 1 - resno);
1588             }
1589         }
1590     }
1591 
1592 #ifdef TODO_MSD /* FIXME */
1593     /* INDEX >>  */
1594     if (p_cstr_info) {
1595         OPJ_UINT32 resno, compno, numprec = 0;
1596         for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1597             opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1598             opj_tccp_t *tccp = &tcp->tccps[compno];
1599             opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1600             for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1601                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1602                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1603                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1604                 numprec += res_idx->pw * res_idx->ph;
1605                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1606                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1607             }
1608         }
1609         p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1610                 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1611         p_cstr_info->packno = 0;
1612     }
1613     /* << INDEX */
1614 #endif
1615 
1616     /*--------------TIER2------------------*/
1617     /* FIXME _ProfStart(PGROUP_T2); */
1618     l_data_read = 0;
1619     if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1620                             p_manager)) {
1621         return OPJ_FALSE;
1622     }
1623     /* FIXME _ProfStop(PGROUP_T2); */
1624 
1625     /*------------------TIER1-----------------*/
1626 
1627     /* FIXME _ProfStart(PGROUP_T1); */
1628     if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1629         return OPJ_FALSE;
1630     }
1631     /* FIXME _ProfStop(PGROUP_T1); */
1632 
1633 
1634     /* For subtile decoding, now we know the resno_decoded, we can allocate */
1635     /* the tile data buffer */
1636     if (!p_tcd->whole_tile_decoding) {
1637         for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1638             opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1639             opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1640             opj_tcd_resolution_t *res = tilec->resolutions + image_comp->resno_decoded;
1641             OPJ_SIZE_T w = res->win_x1 - res->win_x0;
1642             OPJ_SIZE_T h = res->win_y1 - res->win_y0;
1643             OPJ_SIZE_T l_data_size;
1644 
1645             opj_image_data_free(tilec->data_win);
1646             tilec->data_win = NULL;
1647 
1648             if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1649                 continue;
1650             }
1651 
1652             if (w > 0 && h > 0) {
1653                 if (w > SIZE_MAX / h) {
1654                     opj_event_msg(p_manager, EVT_ERROR,
1655                                   "Size of tile data exceeds system limits\n");
1656                     return OPJ_FALSE;
1657                 }
1658                 l_data_size = w * h;
1659                 if (l_data_size > SIZE_MAX / sizeof(OPJ_INT32)) {
1660                     opj_event_msg(p_manager, EVT_ERROR,
1661                                   "Size of tile data exceeds system limits\n");
1662                     return OPJ_FALSE;
1663                 }
1664                 l_data_size *= sizeof(OPJ_INT32);
1665 
1666                 tilec->data_win = (OPJ_INT32*) opj_image_data_alloc(l_data_size);
1667                 if (tilec->data_win == NULL) {
1668                     opj_event_msg(p_manager, EVT_ERROR,
1669                                   "Size of tile data exceeds system limits\n");
1670                     return OPJ_FALSE;
1671                 }
1672             }
1673         }
1674     }
1675 
1676     /*----------------DWT---------------------*/
1677 
1678     /* FIXME _ProfStart(PGROUP_DWT); */
1679     if
1680     (! opj_tcd_dwt_decode(p_tcd)) {
1681         return OPJ_FALSE;
1682     }
1683     /* FIXME _ProfStop(PGROUP_DWT); */
1684 
1685     /*----------------MCT-------------------*/
1686     /* FIXME _ProfStart(PGROUP_MCT); */
1687     if
1688     (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1689         return OPJ_FALSE;
1690     }
1691     /* FIXME _ProfStop(PGROUP_MCT); */
1692 
1693     /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1694     if
1695     (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1696         return OPJ_FALSE;
1697     }
1698     /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1699 
1700 
1701     /*---------------TILE-------------------*/
1702     return OPJ_TRUE;
1703 }
1704 
opj_tcd_update_tile_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_dest,OPJ_UINT32 p_dest_length)1705 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1706                                   OPJ_BYTE * p_dest,
1707                                   OPJ_UINT32 p_dest_length
1708                                  )
1709 {
1710     OPJ_UINT32 i, j, k, l_data_size = 0;
1711     opj_image_comp_t * l_img_comp = 00;
1712     opj_tcd_tilecomp_t * l_tilec = 00;
1713     opj_tcd_resolution_t * l_res;
1714     OPJ_UINT32 l_size_comp, l_remaining;
1715     OPJ_UINT32 l_stride, l_width, l_height;
1716 
1717     l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, OPJ_TRUE);
1718     if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1719         return OPJ_FALSE;
1720     }
1721 
1722     l_tilec = p_tcd->tcd_image->tiles->comps;
1723     l_img_comp = p_tcd->image->comps;
1724 
1725     for (i = 0; i < p_tcd->image->numcomps; ++i) {
1726         const OPJ_INT32* l_src_data;
1727         l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1728         l_remaining = l_img_comp->prec & 7;  /* (%8) */
1729         l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1730         if (p_tcd->whole_tile_decoding) {
1731             l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1732             l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1733             l_stride = (OPJ_UINT32)(l_tilec->resolutions[l_tilec->minimum_num_resolutions -
1734                                                                      1].x1 -
1735                                     l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0) - l_width;
1736             l_src_data = l_tilec->data;
1737         } else {
1738             l_width = l_res->win_x1 - l_res->win_x0;
1739             l_height = l_res->win_y1 - l_res->win_y0;
1740             l_stride = 0;
1741             l_src_data = l_tilec->data_win;
1742         }
1743 
1744         if (l_remaining) {
1745             ++l_size_comp;
1746         }
1747 
1748         if (l_size_comp == 3) {
1749             l_size_comp = 4;
1750         }
1751 
1752         switch (l_size_comp) {
1753         case 1: {
1754             OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1755             const OPJ_INT32 * l_src_ptr = l_src_data;
1756 
1757             if (l_img_comp->sgnd) {
1758                 for (j = 0; j < l_height; ++j) {
1759                     for (k = 0; k < l_width; ++k) {
1760                         *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1761                     }
1762                     l_src_ptr += l_stride;
1763                 }
1764             } else {
1765                 for (j = 0; j < l_height; ++j) {
1766                     for (k = 0; k < l_width; ++k) {
1767                         *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1768                     }
1769                     l_src_ptr += l_stride;
1770                 }
1771             }
1772 
1773             p_dest = (OPJ_BYTE *)l_dest_ptr;
1774         }
1775         break;
1776         case 2: {
1777             const OPJ_INT32 * l_src_ptr = l_src_data;
1778             OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1779 
1780             if (l_img_comp->sgnd) {
1781                 for (j = 0; j < l_height; ++j) {
1782                     for (k = 0; k < l_width; ++k) {
1783                         OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1784                         memcpy(l_dest_ptr, &val, sizeof(val));
1785                         l_dest_ptr ++;
1786                     }
1787                     l_src_ptr += l_stride;
1788                 }
1789             } else {
1790                 for (j = 0; j < l_height; ++j) {
1791                     for (k = 0; k < l_width; ++k) {
1792                         OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1793                         memcpy(l_dest_ptr, &val, sizeof(val));
1794                         l_dest_ptr ++;
1795                     }
1796                     l_src_ptr += l_stride;
1797                 }
1798             }
1799 
1800             p_dest = (OPJ_BYTE*) l_dest_ptr;
1801         }
1802         break;
1803         case 4: {
1804             OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1805             const OPJ_INT32 * l_src_ptr = l_src_data;
1806 
1807             for (j = 0; j < l_height; ++j) {
1808                 memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1809                 l_dest_ptr += l_width;
1810                 l_src_ptr += l_width + l_stride;
1811             }
1812 
1813             p_dest = (OPJ_BYTE*) l_dest_ptr;
1814         }
1815         break;
1816         }
1817 
1818         ++l_img_comp;
1819         ++l_tilec;
1820     }
1821 
1822     return OPJ_TRUE;
1823 }
1824 
1825 
1826 
1827 
opj_tcd_free_tile(opj_tcd_t * p_tcd)1828 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1829 {
1830     OPJ_UINT32 compno, resno, bandno, precno;
1831     opj_tcd_tile_t *l_tile = 00;
1832     opj_tcd_tilecomp_t *l_tile_comp = 00;
1833     opj_tcd_resolution_t *l_res = 00;
1834     opj_tcd_band_t *l_band = 00;
1835     opj_tcd_precinct_t *l_precinct = 00;
1836     OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1837     void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1838 
1839     if (! p_tcd) {
1840         return;
1841     }
1842 
1843     if (! p_tcd->tcd_image) {
1844         return;
1845     }
1846 
1847     if (p_tcd->m_is_decoder) {
1848         l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1849     } else {
1850         l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1851     }
1852 
1853     l_tile = p_tcd->tcd_image->tiles;
1854     if (! l_tile) {
1855         return;
1856     }
1857 
1858     l_tile_comp = l_tile->comps;
1859 
1860     for (compno = 0; compno < l_tile->numcomps; ++compno) {
1861         l_res = l_tile_comp->resolutions;
1862         if (l_res) {
1863 
1864             l_nb_resolutions = l_tile_comp->resolutions_size / (OPJ_UINT32)sizeof(
1865                                    opj_tcd_resolution_t);
1866             for (resno = 0; resno < l_nb_resolutions; ++resno) {
1867                 l_band = l_res->bands;
1868                 for (bandno = 0; bandno < 3; ++bandno) {
1869                     l_precinct = l_band->precincts;
1870                     if (l_precinct) {
1871 
1872                         l_nb_precincts = l_band->precincts_data_size / (OPJ_UINT32)sizeof(
1873                                              opj_tcd_precinct_t);
1874                         for (precno = 0; precno < l_nb_precincts; ++precno) {
1875                             opj_tgt_destroy(l_precinct->incltree);
1876                             l_precinct->incltree = 00;
1877                             opj_tgt_destroy(l_precinct->imsbtree);
1878                             l_precinct->imsbtree = 00;
1879                             (*l_tcd_code_block_deallocate)(l_precinct);
1880                             ++l_precinct;
1881                         }
1882 
1883                         opj_free(l_band->precincts);
1884                         l_band->precincts = 00;
1885                     }
1886                     ++l_band;
1887                 } /* for (resno */
1888                 ++l_res;
1889             }
1890 
1891             opj_free(l_tile_comp->resolutions);
1892             l_tile_comp->resolutions = 00;
1893         }
1894 
1895         if (l_tile_comp->ownsData && l_tile_comp->data) {
1896             opj_image_data_free(l_tile_comp->data);
1897             l_tile_comp->data = 00;
1898             l_tile_comp->ownsData = 0;
1899             l_tile_comp->data_size = 0;
1900             l_tile_comp->data_size_needed = 0;
1901         }
1902 
1903         opj_image_data_free(l_tile_comp->data_win);
1904 
1905         ++l_tile_comp;
1906     }
1907 
1908     opj_free(l_tile->comps);
1909     l_tile->comps = 00;
1910     opj_free(p_tcd->tcd_image->tiles);
1911     p_tcd->tcd_image->tiles = 00;
1912 }
1913 
1914 
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,opj_event_mgr_t * p_manager)1915 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1916                                   OPJ_BYTE * p_src_data,
1917                                   OPJ_UINT32 * p_data_read,
1918                                   OPJ_UINT32 p_max_src_size,
1919                                   opj_codestream_index_t *p_cstr_index,
1920                                   opj_event_mgr_t *p_manager
1921                                  )
1922 {
1923     opj_t2_t * l_t2;
1924 
1925     l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1926     if (l_t2 == 00) {
1927         return OPJ_FALSE;
1928     }
1929 
1930     if (! opj_t2_decode_packets(
1931                 p_tcd,
1932                 l_t2,
1933                 p_tcd->tcd_tileno,
1934                 p_tcd->tcd_image->tiles,
1935                 p_src_data,
1936                 p_data_read,
1937                 p_max_src_size,
1938                 p_cstr_index,
1939                 p_manager)) {
1940         opj_t2_destroy(l_t2);
1941         return OPJ_FALSE;
1942     }
1943 
1944     opj_t2_destroy(l_t2);
1945 
1946     /*---------------CLEAN-------------------*/
1947     return OPJ_TRUE;
1948 }
1949 
opj_tcd_t1_decode(opj_tcd_t * p_tcd,opj_event_mgr_t * p_manager)1950 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1951 {
1952     OPJ_UINT32 compno;
1953     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1954     opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1955     opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1956     volatile OPJ_BOOL ret = OPJ_TRUE;
1957     OPJ_BOOL check_pterm = OPJ_FALSE;
1958     opj_mutex_t* p_manager_mutex = NULL;
1959 
1960     p_manager_mutex = opj_mutex_create();
1961 
1962     /* Only enable PTERM check if we decode all layers */
1963     if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
1964             (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
1965         check_pterm = OPJ_TRUE;
1966     }
1967 
1968     for (compno = 0; compno < l_tile->numcomps;
1969             ++compno, ++l_tile_comp, ++l_tccp) {
1970         if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1971             continue;
1972         }
1973 
1974         opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp,
1975                             p_manager, p_manager_mutex, check_pterm);
1976         if (!ret) {
1977             break;
1978         }
1979     }
1980 
1981     opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
1982     if (p_manager_mutex) {
1983         opj_mutex_destroy(p_manager_mutex);
1984     }
1985     return ret;
1986 }
1987 
1988 
opj_tcd_dwt_decode(opj_tcd_t * p_tcd)1989 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
1990 {
1991     OPJ_UINT32 compno;
1992     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1993     opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1994     opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1995     opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1996 
1997     for (compno = 0; compno < l_tile->numcomps;
1998             compno++, ++l_tile_comp, ++l_img_comp, ++l_tccp) {
1999         if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2000             continue;
2001         }
2002 
2003         if (l_tccp->qmfbid == 1) {
2004             if (! opj_dwt_decode(p_tcd, l_tile_comp,
2005                                  l_img_comp->resno_decoded + 1)) {
2006                 return OPJ_FALSE;
2007             }
2008         } else {
2009             if (! opj_dwt_decode_real(p_tcd, l_tile_comp,
2010                                       l_img_comp->resno_decoded + 1)) {
2011                 return OPJ_FALSE;
2012             }
2013         }
2014 
2015     }
2016 
2017     return OPJ_TRUE;
2018 }
2019 
opj_tcd_mct_decode(opj_tcd_t * p_tcd,opj_event_mgr_t * p_manager)2020 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
2021 {
2022     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2023     opj_tcp_t * l_tcp = p_tcd->tcp;
2024     opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2025     OPJ_UINT32 l_samples, i;
2026 
2027     if (l_tcp->mct == 0 || p_tcd->used_component != NULL) {
2028         return OPJ_TRUE;
2029     }
2030 
2031     if (p_tcd->whole_tile_decoding) {
2032         opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2033                                           l_tile_comp->minimum_num_resolutions - 1;
2034 
2035         /* A bit inefficient: we process more data than needed if */
2036         /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */
2037         /* but we would need to take into account a stride then */
2038         l_samples = (OPJ_UINT32)((res_comp0->x1 - res_comp0->x0) *
2039                                  (res_comp0->y1 - res_comp0->y0));
2040         if (l_tile->numcomps >= 3) {
2041             if (l_tile_comp->minimum_num_resolutions !=
2042                     l_tile->comps[1].minimum_num_resolutions ||
2043                     l_tile_comp->minimum_num_resolutions !=
2044                     l_tile->comps[2].minimum_num_resolutions) {
2045                 opj_event_msg(p_manager, EVT_ERROR,
2046                               "Tiles don't all have the same dimension. Skip the MCT step.\n");
2047                 return OPJ_FALSE;
2048             }
2049         }
2050         if (l_tile->numcomps >= 3) {
2051             opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2052                                               l_tile_comp->minimum_num_resolutions - 1;
2053             opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2054                                               l_tile_comp->minimum_num_resolutions - 1;
2055             /* testcase 1336.pdf.asan.47.376 */
2056             if (p_tcd->image->comps[0].resno_decoded !=
2057                     p_tcd->image->comps[1].resno_decoded ||
2058                     p_tcd->image->comps[0].resno_decoded !=
2059                     p_tcd->image->comps[2].resno_decoded ||
2060                     (OPJ_SIZE_T)(res_comp1->x1 - res_comp1->x0) *
2061                     (OPJ_SIZE_T)(res_comp1->y1 - res_comp1->y0) != l_samples ||
2062                     (OPJ_SIZE_T)(res_comp2->x1 - res_comp2->x0) *
2063                     (OPJ_SIZE_T)(res_comp2->y1 - res_comp2->y0) != l_samples) {
2064                 opj_event_msg(p_manager, EVT_ERROR,
2065                               "Tiles don't all have the same dimension. Skip the MCT step.\n");
2066                 return OPJ_FALSE;
2067             }
2068         }
2069     } else {
2070         opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2071                                           p_tcd->image->comps[0].resno_decoded;
2072 
2073         l_samples = (res_comp0->win_x1 - res_comp0->win_x0) *
2074                     (res_comp0->win_y1 - res_comp0->win_y0);
2075         if (l_tile->numcomps >= 3) {
2076             opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2077                                               p_tcd->image->comps[1].resno_decoded;
2078             opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2079                                               p_tcd->image->comps[2].resno_decoded;
2080             /* testcase 1336.pdf.asan.47.376 */
2081             if (p_tcd->image->comps[0].resno_decoded !=
2082                     p_tcd->image->comps[1].resno_decoded ||
2083                     p_tcd->image->comps[0].resno_decoded !=
2084                     p_tcd->image->comps[2].resno_decoded ||
2085                     (OPJ_SIZE_T)(res_comp1->win_x1 - res_comp1->win_x0) *
2086                     (OPJ_SIZE_T)(res_comp1->win_y1 - res_comp1->win_y0) != l_samples ||
2087                     (OPJ_SIZE_T)(res_comp2->win_x1 - res_comp2->win_x0) *
2088                     (OPJ_SIZE_T)(res_comp2->win_y1 - res_comp2->win_y0) != l_samples) {
2089                 opj_event_msg(p_manager, EVT_ERROR,
2090                               "Tiles don't all have the same dimension. Skip the MCT step.\n");
2091                 return OPJ_FALSE;
2092             }
2093         }
2094     }
2095 
2096     if (l_tile->numcomps >= 3) {
2097         if (l_tcp->mct == 2) {
2098             OPJ_BYTE ** l_data;
2099 
2100             if (! l_tcp->m_mct_decoding_matrix) {
2101                 return OPJ_TRUE;
2102             }
2103 
2104             l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2105             if (! l_data) {
2106                 return OPJ_FALSE;
2107             }
2108 
2109             for (i = 0; i < l_tile->numcomps; ++i) {
2110                 if (p_tcd->whole_tile_decoding) {
2111                     l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2112                 } else {
2113                     l_data[i] = (OPJ_BYTE*) l_tile_comp->data_win;
2114                 }
2115                 ++l_tile_comp;
2116             }
2117 
2118             if (! opj_mct_decode_custom(/* MCT data */
2119                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
2120                         /* size of components */
2121                         l_samples,
2122                         /* components */
2123                         l_data,
2124                         /* nb of components (i.e. size of pData) */
2125                         l_tile->numcomps,
2126                         /* tells if the data is signed */
2127                         p_tcd->image->comps->sgnd)) {
2128                 opj_free(l_data);
2129                 return OPJ_FALSE;
2130             }
2131 
2132             opj_free(l_data);
2133         } else {
2134             if (l_tcp->tccps->qmfbid == 1) {
2135                 if (p_tcd->whole_tile_decoding) {
2136                     opj_mct_decode(l_tile->comps[0].data,
2137                                    l_tile->comps[1].data,
2138                                    l_tile->comps[2].data,
2139                                    l_samples);
2140                 } else {
2141                     opj_mct_decode(l_tile->comps[0].data_win,
2142                                    l_tile->comps[1].data_win,
2143                                    l_tile->comps[2].data_win,
2144                                    l_samples);
2145                 }
2146             } else {
2147                 if (p_tcd->whole_tile_decoding) {
2148                     opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
2149                                         (OPJ_FLOAT32*)l_tile->comps[1].data,
2150                                         (OPJ_FLOAT32*)l_tile->comps[2].data,
2151                                         l_samples);
2152                 } else {
2153                     opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data_win,
2154                                         (OPJ_FLOAT32*)l_tile->comps[1].data_win,
2155                                         (OPJ_FLOAT32*)l_tile->comps[2].data_win,
2156                                         l_samples);
2157                 }
2158             }
2159         }
2160     } else {
2161         opj_event_msg(p_manager, EVT_ERROR,
2162                       "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
2163                       l_tile->numcomps);
2164     }
2165 
2166     return OPJ_TRUE;
2167 }
2168 
2169 
opj_tcd_dc_level_shift_decode(opj_tcd_t * p_tcd)2170 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
2171 {
2172     OPJ_UINT32 compno;
2173     opj_tcd_tilecomp_t * l_tile_comp = 00;
2174     opj_tccp_t * l_tccp = 00;
2175     opj_image_comp_t * l_img_comp = 00;
2176     opj_tcd_resolution_t* l_res = 00;
2177     opj_tcd_tile_t * l_tile;
2178     OPJ_UINT32 l_width, l_height, i, j;
2179     OPJ_INT32 * l_current_ptr;
2180     OPJ_INT32 l_min, l_max;
2181     OPJ_UINT32 l_stride;
2182 
2183     l_tile = p_tcd->tcd_image->tiles;
2184     l_tile_comp = l_tile->comps;
2185     l_tccp = p_tcd->tcp->tccps;
2186     l_img_comp = p_tcd->image->comps;
2187 
2188     for (compno = 0; compno < l_tile->numcomps;
2189             compno++, ++l_img_comp, ++l_tccp, ++l_tile_comp) {
2190 
2191         if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2192             continue;
2193         }
2194 
2195         l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
2196 
2197         if (!p_tcd->whole_tile_decoding) {
2198             l_width = l_res->win_x1 - l_res->win_x0;
2199             l_height = l_res->win_y1 - l_res->win_y0;
2200             l_stride = 0;
2201             l_current_ptr = l_tile_comp->data_win;
2202         } else {
2203             l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
2204             l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
2205             l_stride = (OPJ_UINT32)(
2206                            l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x1 -
2207                            l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x0)
2208                        - l_width;
2209             l_current_ptr = l_tile_comp->data;
2210 
2211             assert(l_height == 0 ||
2212                    l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
2213         }
2214 
2215         if (l_img_comp->sgnd) {
2216             l_min = -(1 << (l_img_comp->prec - 1));
2217             l_max = (1 << (l_img_comp->prec - 1)) - 1;
2218         } else {
2219             l_min = 0;
2220             l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
2221         }
2222 
2223 
2224         if (l_tccp->qmfbid == 1) {
2225             for (j = 0; j < l_height; ++j) {
2226                 for (i = 0; i < l_width; ++i) {
2227                     /* TODO: do addition on int64 ? */
2228                     *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
2229                                                    l_max);
2230                     ++l_current_ptr;
2231                 }
2232                 l_current_ptr += l_stride;
2233             }
2234         } else {
2235             for (j = 0; j < l_height; ++j) {
2236                 for (i = 0; i < l_width; ++i) {
2237                     OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
2238                     if (l_value > INT_MAX) {
2239                         *l_current_ptr = l_max;
2240                     } else if (l_value < INT_MIN) {
2241                         *l_current_ptr = l_min;
2242                     } else {
2243                         /* Do addition on int64 to avoid overflows */
2244                         OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value);
2245                         *l_current_ptr = (OPJ_INT32)opj_int64_clamp(
2246                                              l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
2247                     }
2248                     ++l_current_ptr;
2249                 }
2250                 l_current_ptr += l_stride;
2251             }
2252         }
2253     }
2254 
2255     return OPJ_TRUE;
2256 }
2257 
2258 
2259 
2260 /**
2261  * Deallocates the encoding data of the given precinct.
2262  */
opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)2263 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
2264 {
2265     OPJ_UINT32 cblkno, l_nb_code_blocks;
2266 
2267     opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
2268     if (l_code_block) {
2269         /*fprintf(stderr,"deallocate codeblock:{\n");*/
2270         /*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);*/
2271         /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
2272                         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 );*/
2273 
2274 
2275         l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2276                                opj_tcd_cblk_dec_t);
2277         /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
2278 
2279         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2280 
2281             if (l_code_block->segs) {
2282                 opj_free(l_code_block->segs);
2283                 l_code_block->segs = 00;
2284             }
2285 
2286             if (l_code_block->chunks) {
2287                 opj_free(l_code_block->chunks);
2288                 l_code_block->chunks = 00;
2289             }
2290 
2291             opj_aligned_free(l_code_block->decoded_data);
2292             l_code_block->decoded_data = NULL;
2293 
2294             ++l_code_block;
2295         }
2296 
2297         opj_free(p_precinct->cblks.dec);
2298         p_precinct->cblks.dec = 00;
2299     }
2300 }
2301 
2302 /**
2303  * Deallocates the encoding data of the given precinct.
2304  */
opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)2305 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
2306 {
2307     OPJ_UINT32 cblkno, l_nb_code_blocks;
2308 
2309     opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
2310     if (l_code_block) {
2311         l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2312                                opj_tcd_cblk_enc_t);
2313 
2314         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
2315             if (l_code_block->data) {
2316                 /* We refer to data - 1 since below we incremented it */
2317                 /* in opj_tcd_code_block_enc_allocate_data() */
2318                 opj_free(l_code_block->data - 1);
2319                 l_code_block->data = 00;
2320             }
2321 
2322             if (l_code_block->layers) {
2323                 opj_free(l_code_block->layers);
2324                 l_code_block->layers = 00;
2325             }
2326 
2327             if (l_code_block->passes) {
2328                 opj_free(l_code_block->passes);
2329                 l_code_block->passes = 00;
2330             }
2331             ++l_code_block;
2332         }
2333 
2334         opj_free(p_precinct->cblks.enc);
2335 
2336         p_precinct->cblks.enc = 00;
2337     }
2338 }
2339 
opj_tcd_get_encoded_tile_size(opj_tcd_t * p_tcd)2340 OPJ_SIZE_T opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
2341 {
2342     OPJ_UINT32 i;
2343     OPJ_SIZE_T l_data_size = 0;
2344     opj_image_comp_t * l_img_comp = 00;
2345     opj_tcd_tilecomp_t * l_tilec = 00;
2346     OPJ_UINT32 l_size_comp, l_remaining;
2347 
2348     l_tilec = p_tcd->tcd_image->tiles->comps;
2349     l_img_comp = p_tcd->image->comps;
2350     for (i = 0; i < p_tcd->image->numcomps; ++i) {
2351         l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2352         l_remaining = l_img_comp->prec & 7;  /* (%8) */
2353 
2354         if (l_remaining) {
2355             ++l_size_comp;
2356         }
2357 
2358         if (l_size_comp == 3) {
2359             l_size_comp = 4;
2360         }
2361 
2362         l_data_size += l_size_comp * ((OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2363                                       (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0));
2364         ++l_img_comp;
2365         ++l_tilec;
2366     }
2367 
2368     return l_data_size;
2369 }
2370 
opj_tcd_dc_level_shift_encode(opj_tcd_t * p_tcd)2371 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2372 {
2373     OPJ_UINT32 compno;
2374     opj_tcd_tilecomp_t * l_tile_comp = 00;
2375     opj_tccp_t * l_tccp = 00;
2376     opj_image_comp_t * l_img_comp = 00;
2377     opj_tcd_tile_t * l_tile;
2378     OPJ_SIZE_T l_nb_elem, i;
2379     OPJ_INT32 * l_current_ptr;
2380 
2381     l_tile = p_tcd->tcd_image->tiles;
2382     l_tile_comp = l_tile->comps;
2383     l_tccp = p_tcd->tcp->tccps;
2384     l_img_comp = p_tcd->image->comps;
2385 
2386     for (compno = 0; compno < l_tile->numcomps; compno++) {
2387         l_current_ptr = l_tile_comp->data;
2388         l_nb_elem = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2389                     (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2390 
2391         if (l_tccp->qmfbid == 1) {
2392             for (i = 0; i < l_nb_elem; ++i) {
2393                 *l_current_ptr -= l_tccp->m_dc_level_shift ;
2394                 ++l_current_ptr;
2395             }
2396         } else {
2397             for (i = 0; i < l_nb_elem; ++i) {
2398                 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2399                 ++l_current_ptr;
2400             }
2401         }
2402 
2403         ++l_img_comp;
2404         ++l_tccp;
2405         ++l_tile_comp;
2406     }
2407 
2408     return OPJ_TRUE;
2409 }
2410 
opj_tcd_mct_encode(opj_tcd_t * p_tcd)2411 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2412 {
2413     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2414     opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2415     OPJ_SIZE_T samples = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2416                          (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2417     OPJ_UINT32 i;
2418     OPJ_BYTE ** l_data = 00;
2419     opj_tcp_t * l_tcp = p_tcd->tcp;
2420 
2421     if (!p_tcd->tcp->mct) {
2422         return OPJ_TRUE;
2423     }
2424 
2425     if (p_tcd->tcp->mct == 2) {
2426         if (! p_tcd->tcp->m_mct_coding_matrix) {
2427             return OPJ_TRUE;
2428         }
2429 
2430         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2431         if (! l_data) {
2432             return OPJ_FALSE;
2433         }
2434 
2435         for (i = 0; i < l_tile->numcomps; ++i) {
2436             l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2437             ++l_tile_comp;
2438         }
2439 
2440         if (! opj_mct_encode_custom(/* MCT data */
2441                     (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2442                     /* size of components */
2443                     samples,
2444                     /* components */
2445                     l_data,
2446                     /* nb of components (i.e. size of pData) */
2447                     l_tile->numcomps,
2448                     /* tells if the data is signed */
2449                     p_tcd->image->comps->sgnd)) {
2450             opj_free(l_data);
2451             return OPJ_FALSE;
2452         }
2453 
2454         opj_free(l_data);
2455     } else if (l_tcp->tccps->qmfbid == 0) {
2456         opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2457                             l_tile->comps[2].data, samples);
2458     } else {
2459         opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2460                        l_tile->comps[2].data, samples);
2461     }
2462 
2463     return OPJ_TRUE;
2464 }
2465 
opj_tcd_dwt_encode(opj_tcd_t * p_tcd)2466 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2467 {
2468     opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2469     opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2470     opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2471     OPJ_UINT32 compno;
2472 
2473     for (compno = 0; compno < l_tile->numcomps; ++compno) {
2474         if (l_tccp->qmfbid == 1) {
2475             if (! opj_dwt_encode(l_tile_comp)) {
2476                 return OPJ_FALSE;
2477             }
2478         } else if (l_tccp->qmfbid == 0) {
2479             if (! opj_dwt_encode_real(l_tile_comp)) {
2480                 return OPJ_FALSE;
2481             }
2482         }
2483 
2484         ++l_tile_comp;
2485         ++l_tccp;
2486     }
2487 
2488     return OPJ_TRUE;
2489 }
2490 
opj_tcd_t1_encode(opj_tcd_t * p_tcd)2491 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2492 {
2493     opj_t1_t * l_t1;
2494     const OPJ_FLOAT64 * l_mct_norms;
2495     OPJ_UINT32 l_mct_numcomps = 0U;
2496     opj_tcp_t * l_tcp = p_tcd->tcp;
2497 
2498     l_t1 = opj_t1_create(OPJ_TRUE);
2499     if (l_t1 == 00) {
2500         return OPJ_FALSE;
2501     }
2502 
2503     if (l_tcp->mct == 1) {
2504         l_mct_numcomps = 3U;
2505         /* irreversible encoding */
2506         if (l_tcp->tccps->qmfbid == 0) {
2507             l_mct_norms = opj_mct_get_mct_norms_real();
2508         } else {
2509             l_mct_norms = opj_mct_get_mct_norms();
2510         }
2511     } else {
2512         l_mct_numcomps = p_tcd->image->numcomps;
2513         l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2514     }
2515 
2516     if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2517                               l_mct_numcomps)) {
2518         opj_t1_destroy(l_t1);
2519         return OPJ_FALSE;
2520     }
2521 
2522     opj_t1_destroy(l_t1);
2523 
2524     return OPJ_TRUE;
2525 }
2526 
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,opj_event_mgr_t * p_manager)2527 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2528                                   OPJ_BYTE * p_dest_data,
2529                                   OPJ_UINT32 * p_data_written,
2530                                   OPJ_UINT32 p_max_dest_size,
2531                                   opj_codestream_info_t *p_cstr_info,
2532                                   opj_event_mgr_t *p_manager)
2533 {
2534     opj_t2_t * l_t2;
2535 
2536     l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2537     if (l_t2 == 00) {
2538         return OPJ_FALSE;
2539     }
2540 
2541     if (! opj_t2_encode_packets(
2542                 l_t2,
2543                 p_tcd->tcd_tileno,
2544                 p_tcd->tcd_image->tiles,
2545                 p_tcd->tcp->numlayers,
2546                 p_dest_data,
2547                 p_data_written,
2548                 p_max_dest_size,
2549                 p_cstr_info,
2550                 p_tcd->tp_num,
2551                 p_tcd->tp_pos,
2552                 p_tcd->cur_pino,
2553                 FINAL_PASS,
2554                 p_manager)) {
2555         opj_t2_destroy(l_t2);
2556         return OPJ_FALSE;
2557     }
2558 
2559     opj_t2_destroy(l_t2);
2560 
2561     /*---------------CLEAN-------------------*/
2562     return OPJ_TRUE;
2563 }
2564 
2565 
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,opj_event_mgr_t * p_manager)2566 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2567         OPJ_BYTE * p_dest_data,
2568         OPJ_UINT32 p_max_dest_size,
2569         opj_codestream_info_t *p_cstr_info,
2570         opj_event_mgr_t *p_manager)
2571 {
2572     opj_cp_t * l_cp = p_tcd->cp;
2573     OPJ_UINT32 l_nb_written = 0;
2574 
2575     if (p_cstr_info)  {
2576         p_cstr_info->index_write = 0;
2577     }
2578 
2579     if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2580             l_cp->m_specific_param.m_enc.m_fixed_quality)  {
2581         /* fixed_quality */
2582         /* Normal Rate/distortion allocation */
2583         if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2584                                    p_cstr_info, p_manager)) {
2585             return OPJ_FALSE;
2586         }
2587     } else {
2588         /* Fixed layer allocation */
2589         opj_tcd_rateallocate_fixed(p_tcd);
2590     }
2591 
2592     return OPJ_TRUE;
2593 }
2594 
2595 
opj_tcd_copy_tile_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_src,OPJ_SIZE_T p_src_length)2596 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2597                                 OPJ_BYTE * p_src,
2598                                 OPJ_SIZE_T p_src_length)
2599 {
2600     OPJ_UINT32 i;
2601     OPJ_SIZE_T j;
2602     OPJ_SIZE_T l_data_size = 0;
2603     opj_image_comp_t * l_img_comp = 00;
2604     opj_tcd_tilecomp_t * l_tilec = 00;
2605     OPJ_UINT32 l_size_comp, l_remaining;
2606     OPJ_SIZE_T l_nb_elem;
2607 
2608     l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2609     if (l_data_size != p_src_length) {
2610         return OPJ_FALSE;
2611     }
2612 
2613     l_tilec = p_tcd->tcd_image->tiles->comps;
2614     l_img_comp = p_tcd->image->comps;
2615     for (i = 0; i < p_tcd->image->numcomps; ++i) {
2616         l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2617         l_remaining = l_img_comp->prec & 7;  /* (%8) */
2618         l_nb_elem = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2619                     (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
2620 
2621         if (l_remaining) {
2622             ++l_size_comp;
2623         }
2624 
2625         if (l_size_comp == 3) {
2626             l_size_comp = 4;
2627         }
2628 
2629         switch (l_size_comp) {
2630         case 1: {
2631             OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2632             OPJ_INT32 * l_dest_ptr = l_tilec->data;
2633 
2634             if (l_img_comp->sgnd) {
2635                 for (j = 0; j < l_nb_elem; ++j) {
2636                     *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2637                 }
2638             } else {
2639                 for (j = 0; j < l_nb_elem; ++j) {
2640                     *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2641                 }
2642             }
2643 
2644             p_src = (OPJ_BYTE*) l_src_ptr;
2645         }
2646         break;
2647         case 2: {
2648             OPJ_INT32 * l_dest_ptr = l_tilec->data;
2649             OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2650 
2651             if (l_img_comp->sgnd) {
2652                 for (j = 0; j < l_nb_elem; ++j) {
2653                     *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2654                 }
2655             } else {
2656                 for (j = 0; j < l_nb_elem; ++j) {
2657                     *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2658                 }
2659             }
2660 
2661             p_src = (OPJ_BYTE*) l_src_ptr;
2662         }
2663         break;
2664         case 4: {
2665             OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2666             OPJ_INT32 * l_dest_ptr = l_tilec->data;
2667 
2668             for (j = 0; j < l_nb_elem; ++j) {
2669                 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2670             }
2671 
2672             p_src = (OPJ_BYTE*) l_src_ptr;
2673         }
2674         break;
2675         }
2676 
2677         ++l_img_comp;
2678         ++l_tilec;
2679     }
2680 
2681     return OPJ_TRUE;
2682 }
2683 
opj_tcd_is_band_empty(opj_tcd_band_t * band)2684 OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2685 {
2686     return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);
2687 }
2688 
opj_tcd_is_subband_area_of_interest(opj_tcd_t * tcd,OPJ_UINT32 compno,OPJ_UINT32 resno,OPJ_UINT32 bandno,OPJ_UINT32 band_x0,OPJ_UINT32 band_y0,OPJ_UINT32 band_x1,OPJ_UINT32 band_y1)2689 OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd,
2690         OPJ_UINT32 compno,
2691         OPJ_UINT32 resno,
2692         OPJ_UINT32 bandno,
2693         OPJ_UINT32 band_x0,
2694         OPJ_UINT32 band_y0,
2695         OPJ_UINT32 band_x1,
2696         OPJ_UINT32 band_y1)
2697 {
2698     /* Note: those values for filter_margin are in part the result of */
2699     /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
2700     /* to the maximum left/right extension given in tables F.2 and F.3 of the */
2701     /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
2702     /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
2703     /* needed to be bumped to 4, in case inconsistencies are found while */
2704     /* decoding parts of irreversible coded images. */
2705     /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
2706     OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3;
2707     opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]);
2708     opj_image_comp_t* image_comp = &(tcd->image->comps[compno]);
2709     /* Compute the intersection of the area of interest, expressed in tile coordinates */
2710     /* with the tile coordinates */
2711     OPJ_UINT32 tcx0 = opj_uint_max(
2712                           (OPJ_UINT32)tilec->x0,
2713                           opj_uint_ceildiv(tcd->win_x0, image_comp->dx));
2714     OPJ_UINT32 tcy0 = opj_uint_max(
2715                           (OPJ_UINT32)tilec->y0,
2716                           opj_uint_ceildiv(tcd->win_y0, image_comp->dy));
2717     OPJ_UINT32 tcx1 = opj_uint_min(
2718                           (OPJ_UINT32)tilec->x1,
2719                           opj_uint_ceildiv(tcd->win_x1, image_comp->dx));
2720     OPJ_UINT32 tcy1 = opj_uint_min(
2721                           (OPJ_UINT32)tilec->y1,
2722                           opj_uint_ceildiv(tcd->win_y1, image_comp->dy));
2723     /* Compute number of decomposition for this band. See table F-1 */
2724     OPJ_UINT32 nb = (resno == 0) ?
2725                     tilec->numresolutions - 1 :
2726                     tilec->numresolutions - resno;
2727     /* Map above tile-based coordinates to sub-band-based coordinates per */
2728     /* equation B-15 of the standard */
2729     OPJ_UINT32 x0b = bandno & 1;
2730     OPJ_UINT32 y0b = bandno >> 1;
2731     OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 :
2732                       (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2733                       opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2734     OPJ_UINT32 tby0 = (nb == 0) ? tcy0 :
2735                       (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2736                       opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2737     OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 :
2738                       (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2739                       opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2740     OPJ_UINT32 tby1 = (nb == 0) ? tcy1 :
2741                       (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2742                       opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2743     OPJ_BOOL intersects;
2744 
2745     if (tbx0 < filter_margin) {
2746         tbx0 = 0;
2747     } else {
2748         tbx0 -= filter_margin;
2749     }
2750     if (tby0 < filter_margin) {
2751         tby0 = 0;
2752     } else {
2753         tby0 -= filter_margin;
2754     }
2755     tbx1 = opj_uint_adds(tbx1, filter_margin);
2756     tby1 = opj_uint_adds(tby1, filter_margin);
2757 
2758     intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 &&
2759                  band_y1 > tby0;
2760 
2761 #ifdef DEBUG_VERBOSE
2762     printf("compno=%u resno=%u nb=%u bandno=%u x0b=%u y0b=%u band=%u,%u,%u,%u tb=%u,%u,%u,%u -> %u\n",
2763            compno, resno, nb, bandno, x0b, y0b,
2764            band_x0, band_y0, band_x1, band_y1,
2765            tbx0, tby0, tbx1, tby1, intersects);
2766 #endif
2767     return intersects;
2768 }
2769 
2770 /** Returns whether a tile componenent is fully decoded, taking into account
2771  * p_tcd->win_* members.
2772  *
2773  * @param p_tcd    TCD handle.
2774  * @param compno Component number
2775  * @return OPJ_TRUE whether the tile componenent is fully decoded
2776  */
opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t * p_tcd,OPJ_UINT32 compno)2777 static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *p_tcd,
2778         OPJ_UINT32 compno)
2779 {
2780     opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
2781     opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
2782     /* Compute the intersection of the area of interest, expressed in tile coordinates */
2783     /* with the tile coordinates */
2784     OPJ_UINT32 tcx0 = opj_uint_max(
2785                           (OPJ_UINT32)tilec->x0,
2786                           opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
2787     OPJ_UINT32 tcy0 = opj_uint_max(
2788                           (OPJ_UINT32)tilec->y0,
2789                           opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
2790     OPJ_UINT32 tcx1 = opj_uint_min(
2791                           (OPJ_UINT32)tilec->x1,
2792                           opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
2793     OPJ_UINT32 tcy1 = opj_uint_min(
2794                           (OPJ_UINT32)tilec->y1,
2795                           opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
2796 
2797     OPJ_UINT32 shift = tilec->numresolutions - tilec->minimum_num_resolutions;
2798     /* Tolerate small margin within the reduced resolution factor to consider if */
2799     /* the whole tile path must be taken */
2800     return (tcx0 >= (OPJ_UINT32)tilec->x0 &&
2801             tcy0 >= (OPJ_UINT32)tilec->y0 &&
2802             tcx1 <= (OPJ_UINT32)tilec->x1 &&
2803             tcy1 <= (OPJ_UINT32)tilec->y1 &&
2804             (shift >= 32 ||
2805              (((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 &&
2806               ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 &&
2807               (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 &&
2808               (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0)));
2809 }
2810