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 {
914 OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->x1,
915 (OPJ_INT32)l_pdx)) << l_pdx;
916 if (tmp > (OPJ_UINT32)INT_MAX) {
917 opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
918 return OPJ_FALSE;
919 }
920 l_br_prc_x_end = (OPJ_INT32)tmp;
921 }
922 {
923 OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->y1,
924 (OPJ_INT32)l_pdy)) << l_pdy;
925 if (tmp > (OPJ_UINT32)INT_MAX) {
926 opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
927 return OPJ_FALSE;
928 }
929 l_br_prc_y_end = (OPJ_INT32)tmp;
930 }
931 /*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 );*/
932
933 l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
934 l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
935 l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
936 l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
937 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
938
939 if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
940 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
941 return OPJ_FALSE;
942 }
943 l_nb_precincts = l_res->pw * l_res->ph;
944
945 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
946 l_nb_precincts) {
947 opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
948 return OPJ_FALSE;
949 }
950 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
951
952 if (resno == 0) {
953 tlcbgxstart = l_tl_prc_x_start;
954 tlcbgystart = l_tl_prc_y_start;
955 /*brcbgxend = l_br_prc_x_end;*/
956 /* brcbgyend = l_br_prc_y_end;*/
957 cbgwidthexpn = l_pdx;
958 cbgheightexpn = l_pdy;
959 l_res->numbands = 1;
960 } else {
961 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
962 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
963 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
964 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
965 cbgwidthexpn = l_pdx - 1;
966 cbgheightexpn = l_pdy - 1;
967 l_res->numbands = 3;
968 }
969
970 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
971 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
972 l_band = l_res->bands;
973
974 for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
975 OPJ_INT32 numbps;
976 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
977
978 if (resno == 0) {
979 l_band->bandno = 0 ;
980 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
981 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
982 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
983 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
984 } else {
985 l_band->bandno = bandno + 1;
986 /* x0b = 1 if bandno = 1 or 3 */
987 l_x0b = l_band->bandno & 1;
988 /* y0b = 1 if bandno = 2 or 3 */
989 l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
990 /* l_band border (global) */
991 l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
992 l_level_no), (OPJ_INT32)(l_level_no + 1));
993 l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
994 l_level_no), (OPJ_INT32)(l_level_no + 1));
995 l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
996 l_level_no), (OPJ_INT32)(l_level_no + 1));
997 l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
998 l_level_no), (OPJ_INT32)(l_level_no + 1));
999 }
1000
1001 if (isEncoder) {
1002 /* Skip empty bands */
1003 if (opj_tcd_is_band_empty(l_band)) {
1004 /* Do not zero l_band->precints to avoid leaks */
1005 /* but make sure we don't use it later, since */
1006 /* it will point to precincts of previous bands... */
1007 continue;
1008 }
1009 }
1010
1011 /** avoid an if with storing function pointer */
1012 l_gain = (*l_gain_ptr)(l_band->bandno);
1013 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
1014 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
1015 (OPJ_INT32)(numbps - l_step_size->expn)))) * fraction;
1016 /* Mb value of Equation E-2 in "E.1 Inverse quantization
1017 * procedure" of the standard */
1018 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
1019 1;
1020
1021 if (!l_band->precincts && (l_nb_precincts > 0U)) {
1022 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
1023 l_nb_precinct_size);
1024 if (! l_band->precincts) {
1025 opj_event_msg(manager, EVT_ERROR,
1026 "Not enough memory to handle band precints\n");
1027 return OPJ_FALSE;
1028 }
1029 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */
1030 memset(l_band->precincts, 0, l_nb_precinct_size);
1031 l_band->precincts_data_size = l_nb_precinct_size;
1032 } else if (l_band->precincts_data_size < l_nb_precinct_size) {
1033
1034 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
1035 l_band->precincts,/*3 * */ l_nb_precinct_size);
1036 if (! new_precincts) {
1037 opj_event_msg(manager, EVT_ERROR,
1038 "Not enough memory to handle band precints\n");
1039 opj_free(l_band->precincts);
1040 l_band->precincts = NULL;
1041 l_band->precincts_data_size = 0;
1042 return OPJ_FALSE;
1043 }
1044 l_band->precincts = new_precincts;
1045 /*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);*/
1046 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
1047 l_nb_precinct_size - l_band->precincts_data_size);
1048 l_band->precincts_data_size = l_nb_precinct_size;
1049 }
1050
1051 l_current_precinct = l_band->precincts;
1052 for (precno = 0; precno < l_nb_precincts; ++precno) {
1053 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1054 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1055 (1 << cbgwidthexpn);
1056 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1057 (1 << cbgheightexpn);
1058 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1059 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1060 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1061 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1062
1063 /* precinct size (global) */
1064 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1065
1066 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1067 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1068 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1069 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1070 /*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);*/
1071
1072 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1073 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1074 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1075 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1076 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1077 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1078 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1079 (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1080 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1081 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1082 (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1083 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1084 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1085 cblkwidthexpn);
1086 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1087 cblkheightexpn);
1088
1089 if (l_current_precinct->cw && ((OPJ_UINT32)-1) / l_current_precinct->cw < l_current_precinct->ch) {
1090 return OPJ_FALSE;
1091 }
1092 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1093 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */
1094 if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof_block) <
1095 l_nb_code_blocks) {
1096 opj_event_msg(manager, EVT_ERROR,
1097 "Size of code block data exceeds system limits\n");
1098 return OPJ_FALSE;
1099 }
1100 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1101
1102 if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1103 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1104 if (! l_current_precinct->cblks.blocks) {
1105 return OPJ_FALSE;
1106 }
1107 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1108
1109 memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1110
1111 l_current_precinct->block_size = l_nb_code_blocks_size;
1112 } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1113 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1114 l_nb_code_blocks_size);
1115 if (! new_blocks) {
1116 opj_free(l_current_precinct->cblks.blocks);
1117 l_current_precinct->cblks.blocks = NULL;
1118 l_current_precinct->block_size = 0;
1119 opj_event_msg(manager, EVT_ERROR,
1120 "Not enough memory for current precinct codeblock element\n");
1121 return OPJ_FALSE;
1122 }
1123 l_current_precinct->cblks.blocks = new_blocks;
1124 /*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); */
1125
1126 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1127 l_current_precinct->block_size
1128 , 0
1129 , l_nb_code_blocks_size - l_current_precinct->block_size);
1130
1131 l_current_precinct->block_size = l_nb_code_blocks_size;
1132 }
1133
1134 if (! l_current_precinct->incltree) {
1135 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1136 l_current_precinct->ch, manager);
1137 } else {
1138 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1139 l_current_precinct->cw, l_current_precinct->ch, manager);
1140 }
1141
1142 if (! l_current_precinct->imsbtree) {
1143 l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1144 l_current_precinct->ch, manager);
1145 } else {
1146 l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1147 l_current_precinct->cw, l_current_precinct->ch, manager);
1148 }
1149
1150 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1151 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1152 l_current_precinct->cw) * (1 << cblkwidthexpn);
1153 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1154 l_current_precinct->cw) * (1 << cblkheightexpn);
1155 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1156 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1157
1158 if (isEncoder) {
1159 opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1160
1161 if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1162 return OPJ_FALSE;
1163 }
1164 /* code-block size (global) */
1165 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1166 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1167 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1168 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1169
1170 if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1171 return OPJ_FALSE;
1172 }
1173 } else {
1174 opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1175
1176 if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1177 return OPJ_FALSE;
1178 }
1179 /* code-block size (global) */
1180 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1181 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1182 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1183 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1184 }
1185 }
1186 ++l_current_precinct;
1187 } /* precno */
1188 } /* bandno */
1189 ++l_res;
1190 } /* resno */
1191 ++l_tccp;
1192 ++l_tilec;
1193 ++l_image_comp;
1194 } /* compno */
1195 return OPJ_TRUE;
1196 }
1197
opj_tcd_init_encode_tile(opj_tcd_t * p_tcd,OPJ_UINT32 p_tile_no,opj_event_mgr_t * p_manager)1198 OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1199 opj_event_mgr_t* p_manager)
1200 {
1201 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F,
1202 sizeof(opj_tcd_cblk_enc_t), p_manager);
1203 }
1204
opj_tcd_init_decode_tile(opj_tcd_t * p_tcd,OPJ_UINT32 p_tile_no,opj_event_mgr_t * p_manager)1205 OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1206 opj_event_mgr_t* p_manager)
1207 {
1208 return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F,
1209 sizeof(opj_tcd_cblk_dec_t), p_manager);
1210 }
1211
1212 /**
1213 * Allocates memory for an encoding code block (but not data memory).
1214 */
opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t * p_code_block)1215 static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1216 p_code_block)
1217 {
1218 if (! p_code_block->layers) {
1219 /* no memset since data */
1220 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1221 sizeof(opj_tcd_layer_t));
1222 if (! p_code_block->layers) {
1223 return OPJ_FALSE;
1224 }
1225 }
1226 if (! p_code_block->passes) {
1227 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1228 sizeof(opj_tcd_pass_t));
1229 if (! p_code_block->passes) {
1230 return OPJ_FALSE;
1231 }
1232 }
1233 return OPJ_TRUE;
1234 }
1235
1236 /**
1237 * Allocates data memory for an encoding code block.
1238 */
opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t * p_code_block)1239 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1240 p_code_block)
1241 {
1242 OPJ_UINT32 l_data_size;
1243
1244 /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1245 /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
1246 /* TODO: is there a theoretical upper-bound for the compressed code */
1247 /* block size ? */
1248 l_data_size = 2 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1249 (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1250
1251 if (l_data_size > p_code_block->data_size) {
1252 if (p_code_block->data) {
1253 /* We refer to data - 1 since below we incremented it */
1254 opj_free(p_code_block->data - 1);
1255 }
1256 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1257 if (! p_code_block->data) {
1258 p_code_block->data_size = 0U;
1259 return OPJ_FALSE;
1260 }
1261 p_code_block->data_size = l_data_size;
1262
1263 /* We reserve the initial byte as a fake byte to a non-FF value */
1264 /* and increment the data pointer, so that opj_mqc_init_enc() */
1265 /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1266 /* it. */
1267 p_code_block->data[0] = 0;
1268 p_code_block->data += 1; /*why +1 ?*/
1269 }
1270 return OPJ_TRUE;
1271 }
1272
1273
opj_tcd_reinit_segment(opj_tcd_seg_t * seg)1274 void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1275 {
1276 memset(seg, 0, sizeof(opj_tcd_seg_t));
1277 }
1278
1279 /**
1280 * Allocates memory for a decoding code block.
1281 */
opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t * p_code_block)1282 static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1283 p_code_block)
1284 {
1285 if (! p_code_block->segs) {
1286
1287 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1288 sizeof(opj_tcd_seg_t));
1289 if (! p_code_block->segs) {
1290 return OPJ_FALSE;
1291 }
1292 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1293
1294 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1295 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1296 } else {
1297 /* sanitize */
1298 opj_tcd_seg_t * l_segs = p_code_block->segs;
1299 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1300 opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
1301 OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
1302 OPJ_UINT32 i;
1303
1304 opj_aligned_free(p_code_block->decoded_data);
1305 p_code_block->decoded_data = 00;
1306
1307 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1308 p_code_block->segs = l_segs;
1309 p_code_block->m_current_max_segs = l_current_max_segs;
1310 for (i = 0; i < l_current_max_segs; ++i) {
1311 opj_tcd_reinit_segment(&l_segs[i]);
1312 }
1313 p_code_block->chunks = l_chunks;
1314 p_code_block->numchunksalloc = l_numchunksalloc;
1315 }
1316
1317 return OPJ_TRUE;
1318 }
1319
opj_tcd_get_decoded_tile_size(opj_tcd_t * p_tcd,OPJ_BOOL take_into_account_partial_decoding)1320 OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd,
1321 OPJ_BOOL take_into_account_partial_decoding)
1322 {
1323 OPJ_UINT32 i;
1324 OPJ_UINT32 l_data_size = 0;
1325 opj_image_comp_t * l_img_comp = 00;
1326 opj_tcd_tilecomp_t * l_tile_comp = 00;
1327 opj_tcd_resolution_t * l_res = 00;
1328 OPJ_UINT32 l_size_comp, l_remaining;
1329 OPJ_UINT32 l_temp;
1330
1331 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1332 l_img_comp = p_tcd->image->comps;
1333
1334 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1335 OPJ_UINT32 w, h;
1336 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1337 l_remaining = l_img_comp->prec & 7; /* (%8) */
1338
1339 if (l_remaining) {
1340 ++l_size_comp;
1341 }
1342
1343 if (l_size_comp == 3) {
1344 l_size_comp = 4;
1345 }
1346
1347 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1348 if (take_into_account_partial_decoding && !p_tcd->whole_tile_decoding) {
1349 w = l_res->win_x1 - l_res->win_x0;
1350 h = l_res->win_y1 - l_res->win_y0;
1351 } else {
1352 w = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1353 h = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1354 }
1355 if (h > 0 && UINT_MAX / w < h) {
1356 return UINT_MAX;
1357 }
1358 l_temp = w * h;
1359 if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1360 return UINT_MAX;
1361 }
1362 l_temp *= l_size_comp;
1363
1364 if (l_temp > UINT_MAX - l_data_size) {
1365 return UINT_MAX;
1366 }
1367 l_data_size += l_temp;
1368 ++l_img_comp;
1369 ++l_tile_comp;
1370 }
1371
1372 return l_data_size;
1373 }
1374
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)1375 OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1376 OPJ_UINT32 p_tile_no,
1377 OPJ_BYTE *p_dest,
1378 OPJ_UINT32 * p_data_written,
1379 OPJ_UINT32 p_max_length,
1380 opj_codestream_info_t *p_cstr_info,
1381 opj_event_mgr_t *p_manager)
1382 {
1383
1384 if (p_tcd->cur_tp_num == 0) {
1385
1386 p_tcd->tcd_tileno = p_tile_no;
1387 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1388
1389 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1390 if (p_cstr_info) {
1391 OPJ_UINT32 l_num_packs = 0;
1392 OPJ_UINT32 i;
1393 opj_tcd_tilecomp_t *l_tilec_idx =
1394 &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1395 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1396
1397 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1398 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1399
1400 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1401 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1402
1403 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1404 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1405 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1406 }
1407 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1408 OPJ_SIZE_T)p_cstr_info->numcomps * (OPJ_SIZE_T)p_cstr_info->numlayers *
1409 l_num_packs,
1410 sizeof(opj_packet_info_t));
1411 if (!p_cstr_info->tile[p_tile_no].packet) {
1412 /* FIXME event manager error callback */
1413 return OPJ_FALSE;
1414 }
1415 }
1416 /* << INDEX */
1417
1418 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1419 /*---------------TILE-------------------*/
1420 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1421 return OPJ_FALSE;
1422 }
1423 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1424
1425 /* FIXME _ProfStart(PGROUP_MCT); */
1426 if (! opj_tcd_mct_encode(p_tcd)) {
1427 return OPJ_FALSE;
1428 }
1429 /* FIXME _ProfStop(PGROUP_MCT); */
1430
1431 /* FIXME _ProfStart(PGROUP_DWT); */
1432 if (! opj_tcd_dwt_encode(p_tcd)) {
1433 return OPJ_FALSE;
1434 }
1435 /* FIXME _ProfStop(PGROUP_DWT); */
1436
1437 /* FIXME _ProfStart(PGROUP_T1); */
1438 if (! opj_tcd_t1_encode(p_tcd)) {
1439 return OPJ_FALSE;
1440 }
1441 /* FIXME _ProfStop(PGROUP_T1); */
1442
1443 /* FIXME _ProfStart(PGROUP_RATE); */
1444 if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
1445 p_cstr_info, p_manager)) {
1446 return OPJ_FALSE;
1447 }
1448 /* FIXME _ProfStop(PGROUP_RATE); */
1449
1450 }
1451 /*--------------TIER2------------------*/
1452
1453 /* INDEX */
1454 if (p_cstr_info) {
1455 p_cstr_info->index_write = 1;
1456 }
1457 /* FIXME _ProfStart(PGROUP_T2); */
1458
1459 if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1460 p_cstr_info, p_manager)) {
1461 return OPJ_FALSE;
1462 }
1463 /* FIXME _ProfStop(PGROUP_T2); */
1464
1465 /*---------------CLEAN-------------------*/
1466
1467 return OPJ_TRUE;
1468 }
1469
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)1470 OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1471 OPJ_UINT32 win_x0,
1472 OPJ_UINT32 win_y0,
1473 OPJ_UINT32 win_x1,
1474 OPJ_UINT32 win_y1,
1475 OPJ_UINT32 numcomps_to_decode,
1476 const OPJ_UINT32 *comps_indices,
1477 OPJ_BYTE *p_src,
1478 OPJ_UINT32 p_max_length,
1479 OPJ_UINT32 p_tile_no,
1480 opj_codestream_index_t *p_cstr_index,
1481 opj_event_mgr_t *p_manager
1482 )
1483 {
1484 OPJ_UINT32 l_data_read;
1485 OPJ_UINT32 compno;
1486
1487 p_tcd->tcd_tileno = p_tile_no;
1488 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1489 p_tcd->win_x0 = win_x0;
1490 p_tcd->win_y0 = win_y0;
1491 p_tcd->win_x1 = win_x1;
1492 p_tcd->win_y1 = win_y1;
1493 p_tcd->whole_tile_decoding = OPJ_TRUE;
1494
1495 opj_free(p_tcd->used_component);
1496 p_tcd->used_component = NULL;
1497
1498 if (numcomps_to_decode) {
1499 OPJ_BOOL* used_component = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
1500 p_tcd->image->numcomps);
1501 if (used_component == NULL) {
1502 return OPJ_FALSE;
1503 }
1504 for (compno = 0; compno < numcomps_to_decode; compno++) {
1505 used_component[ comps_indices[compno] ] = OPJ_TRUE;
1506 }
1507
1508 p_tcd->used_component = used_component;
1509 }
1510
1511 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1512 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1513 continue;
1514 }
1515
1516 if (!opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno)) {
1517 p_tcd->whole_tile_decoding = OPJ_FALSE;
1518 break;
1519 }
1520 }
1521
1522 if (p_tcd->whole_tile_decoding) {
1523 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1524 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1525 opj_tcd_resolution_t *l_res = &
1526 (tilec->resolutions[tilec->minimum_num_resolutions - 1]);
1527 OPJ_SIZE_T l_data_size;
1528
1529 /* compute l_data_size with overflow check */
1530 OPJ_SIZE_T res_w = (OPJ_SIZE_T)(l_res->x1 - l_res->x0);
1531 OPJ_SIZE_T res_h = (OPJ_SIZE_T)(l_res->y1 - l_res->y0);
1532
1533 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1534 continue;
1535 }
1536
1537 /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
1538 if (res_h > 0 && res_w > SIZE_MAX / res_h) {
1539 opj_event_msg(p_manager, EVT_ERROR,
1540 "Size of tile data exceeds system limits\n");
1541 return OPJ_FALSE;
1542 }
1543 l_data_size = res_w * res_h;
1544
1545 if (SIZE_MAX / sizeof(OPJ_UINT32) < l_data_size) {
1546 opj_event_msg(p_manager, EVT_ERROR,
1547 "Size of tile data exceeds system limits\n");
1548 return OPJ_FALSE;
1549 }
1550 l_data_size *= sizeof(OPJ_UINT32);
1551
1552 tilec->data_size_needed = l_data_size;
1553
1554 if (!opj_alloc_tile_component_data(tilec)) {
1555 opj_event_msg(p_manager, EVT_ERROR,
1556 "Size of tile data exceeds system limits\n");
1557 return OPJ_FALSE;
1558 }
1559 }
1560 } else {
1561 /* Compute restricted tile-component and tile-resolution coordinates */
1562 /* of the window of interest, but defer the memory allocation until */
1563 /* we know the resno_decoded */
1564 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1565 OPJ_UINT32 resno;
1566 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1567 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1568
1569 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1570 continue;
1571 }
1572
1573 /* Compute the intersection of the area of interest, expressed in tile coordinates */
1574 /* with the tile coordinates */
1575 tilec->win_x0 = opj_uint_max(
1576 (OPJ_UINT32)tilec->x0,
1577 opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
1578 tilec->win_y0 = opj_uint_max(
1579 (OPJ_UINT32)tilec->y0,
1580 opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
1581 tilec->win_x1 = opj_uint_min(
1582 (OPJ_UINT32)tilec->x1,
1583 opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
1584 tilec->win_y1 = opj_uint_min(
1585 (OPJ_UINT32)tilec->y1,
1586 opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
1587 if (tilec->win_x1 < tilec->win_x0 ||
1588 tilec->win_y1 < tilec->win_y0) {
1589 /* We should not normally go there. The circumstance is when */
1590 /* the tile coordinates do not intersect the area of interest */
1591 /* Upper level logic should not even try to decode that tile */
1592 opj_event_msg(p_manager, EVT_ERROR,
1593 "Invalid tilec->win_xxx values\n");
1594 return OPJ_FALSE;
1595 }
1596
1597 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1598 opj_tcd_resolution_t *res = tilec->resolutions + resno;
1599 res->win_x0 = opj_uint_ceildivpow2(tilec->win_x0,
1600 tilec->numresolutions - 1 - resno);
1601 res->win_y0 = opj_uint_ceildivpow2(tilec->win_y0,
1602 tilec->numresolutions - 1 - resno);
1603 res->win_x1 = opj_uint_ceildivpow2(tilec->win_x1,
1604 tilec->numresolutions - 1 - resno);
1605 res->win_y1 = opj_uint_ceildivpow2(tilec->win_y1,
1606 tilec->numresolutions - 1 - resno);
1607 }
1608 }
1609 }
1610
1611 #ifdef TODO_MSD /* FIXME */
1612 /* INDEX >> */
1613 if (p_cstr_info) {
1614 OPJ_UINT32 resno, compno, numprec = 0;
1615 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1616 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1617 opj_tccp_t *tccp = &tcp->tccps[compno];
1618 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1619 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1620 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1621 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1622 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1623 numprec += res_idx->pw * res_idx->ph;
1624 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1625 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1626 }
1627 }
1628 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1629 p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1630 p_cstr_info->packno = 0;
1631 }
1632 /* << INDEX */
1633 #endif
1634
1635 /*--------------TIER2------------------*/
1636 /* FIXME _ProfStart(PGROUP_T2); */
1637 l_data_read = 0;
1638 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1639 p_manager)) {
1640 return OPJ_FALSE;
1641 }
1642 /* FIXME _ProfStop(PGROUP_T2); */
1643
1644 /*------------------TIER1-----------------*/
1645
1646 /* FIXME _ProfStart(PGROUP_T1); */
1647 if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1648 return OPJ_FALSE;
1649 }
1650 /* FIXME _ProfStop(PGROUP_T1); */
1651
1652
1653 /* For subtile decoding, now we know the resno_decoded, we can allocate */
1654 /* the tile data buffer */
1655 if (!p_tcd->whole_tile_decoding) {
1656 for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1657 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1658 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1659 opj_tcd_resolution_t *res = tilec->resolutions + image_comp->resno_decoded;
1660 OPJ_SIZE_T w = res->win_x1 - res->win_x0;
1661 OPJ_SIZE_T h = res->win_y1 - res->win_y0;
1662 OPJ_SIZE_T l_data_size;
1663
1664 opj_image_data_free(tilec->data_win);
1665 tilec->data_win = NULL;
1666
1667 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1668 continue;
1669 }
1670
1671 if (w > 0 && h > 0) {
1672 if (w > SIZE_MAX / h) {
1673 opj_event_msg(p_manager, EVT_ERROR,
1674 "Size of tile data exceeds system limits\n");
1675 return OPJ_FALSE;
1676 }
1677 l_data_size = w * h;
1678 if (l_data_size > SIZE_MAX / sizeof(OPJ_INT32)) {
1679 opj_event_msg(p_manager, EVT_ERROR,
1680 "Size of tile data exceeds system limits\n");
1681 return OPJ_FALSE;
1682 }
1683 l_data_size *= sizeof(OPJ_INT32);
1684
1685 tilec->data_win = (OPJ_INT32*) opj_image_data_alloc(l_data_size);
1686 if (tilec->data_win == NULL) {
1687 opj_event_msg(p_manager, EVT_ERROR,
1688 "Size of tile data exceeds system limits\n");
1689 return OPJ_FALSE;
1690 }
1691 }
1692 }
1693 }
1694
1695 /*----------------DWT---------------------*/
1696
1697 /* FIXME _ProfStart(PGROUP_DWT); */
1698 if
1699 (! opj_tcd_dwt_decode(p_tcd)) {
1700 return OPJ_FALSE;
1701 }
1702 /* FIXME _ProfStop(PGROUP_DWT); */
1703
1704 /*----------------MCT-------------------*/
1705 /* FIXME _ProfStart(PGROUP_MCT); */
1706 if
1707 (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1708 return OPJ_FALSE;
1709 }
1710 /* FIXME _ProfStop(PGROUP_MCT); */
1711
1712 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1713 if
1714 (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1715 return OPJ_FALSE;
1716 }
1717 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1718
1719
1720 /*---------------TILE-------------------*/
1721 return OPJ_TRUE;
1722 }
1723
opj_tcd_update_tile_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_dest,OPJ_UINT32 p_dest_length)1724 OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1725 OPJ_BYTE * p_dest,
1726 OPJ_UINT32 p_dest_length
1727 )
1728 {
1729 OPJ_UINT32 i, j, k, l_data_size = 0;
1730 opj_image_comp_t * l_img_comp = 00;
1731 opj_tcd_tilecomp_t * l_tilec = 00;
1732 opj_tcd_resolution_t * l_res;
1733 OPJ_UINT32 l_size_comp, l_remaining;
1734 OPJ_UINT32 l_stride, l_width, l_height;
1735
1736 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, OPJ_TRUE);
1737 if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1738 return OPJ_FALSE;
1739 }
1740
1741 l_tilec = p_tcd->tcd_image->tiles->comps;
1742 l_img_comp = p_tcd->image->comps;
1743
1744 for (i = 0; i < p_tcd->image->numcomps; ++i) {
1745 const OPJ_INT32* l_src_data;
1746 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1747 l_remaining = l_img_comp->prec & 7; /* (%8) */
1748 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1749 if (p_tcd->whole_tile_decoding) {
1750 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1751 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1752 l_stride = (OPJ_UINT32)(l_tilec->resolutions[l_tilec->minimum_num_resolutions -
1753 1].x1 -
1754 l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0) - l_width;
1755 l_src_data = l_tilec->data;
1756 } else {
1757 l_width = l_res->win_x1 - l_res->win_x0;
1758 l_height = l_res->win_y1 - l_res->win_y0;
1759 l_stride = 0;
1760 l_src_data = l_tilec->data_win;
1761 }
1762
1763 if (l_remaining) {
1764 ++l_size_comp;
1765 }
1766
1767 if (l_size_comp == 3) {
1768 l_size_comp = 4;
1769 }
1770
1771 switch (l_size_comp) {
1772 case 1: {
1773 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1774 const OPJ_INT32 * l_src_ptr = l_src_data;
1775
1776 if (l_img_comp->sgnd) {
1777 for (j = 0; j < l_height; ++j) {
1778 for (k = 0; k < l_width; ++k) {
1779 *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1780 }
1781 l_src_ptr += l_stride;
1782 }
1783 } else {
1784 for (j = 0; j < l_height; ++j) {
1785 for (k = 0; k < l_width; ++k) {
1786 *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1787 }
1788 l_src_ptr += l_stride;
1789 }
1790 }
1791
1792 p_dest = (OPJ_BYTE *)l_dest_ptr;
1793 }
1794 break;
1795 case 2: {
1796 const OPJ_INT32 * l_src_ptr = l_src_data;
1797 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1798
1799 if (l_img_comp->sgnd) {
1800 for (j = 0; j < l_height; ++j) {
1801 for (k = 0; k < l_width; ++k) {
1802 OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1803 memcpy(l_dest_ptr, &val, sizeof(val));
1804 l_dest_ptr ++;
1805 }
1806 l_src_ptr += l_stride;
1807 }
1808 } else {
1809 for (j = 0; j < l_height; ++j) {
1810 for (k = 0; k < l_width; ++k) {
1811 OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1812 memcpy(l_dest_ptr, &val, sizeof(val));
1813 l_dest_ptr ++;
1814 }
1815 l_src_ptr += l_stride;
1816 }
1817 }
1818
1819 p_dest = (OPJ_BYTE*) l_dest_ptr;
1820 }
1821 break;
1822 case 4: {
1823 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1824 const OPJ_INT32 * l_src_ptr = l_src_data;
1825
1826 for (j = 0; j < l_height; ++j) {
1827 memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1828 l_dest_ptr += l_width;
1829 l_src_ptr += l_width + l_stride;
1830 }
1831
1832 p_dest = (OPJ_BYTE*) l_dest_ptr;
1833 }
1834 break;
1835 }
1836
1837 ++l_img_comp;
1838 ++l_tilec;
1839 }
1840
1841 return OPJ_TRUE;
1842 }
1843
1844
1845
1846
opj_tcd_free_tile(opj_tcd_t * p_tcd)1847 static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1848 {
1849 OPJ_UINT32 compno, resno, bandno, precno;
1850 opj_tcd_tile_t *l_tile = 00;
1851 opj_tcd_tilecomp_t *l_tile_comp = 00;
1852 opj_tcd_resolution_t *l_res = 00;
1853 opj_tcd_band_t *l_band = 00;
1854 opj_tcd_precinct_t *l_precinct = 00;
1855 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1856 void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1857
1858 if (! p_tcd) {
1859 return;
1860 }
1861
1862 if (! p_tcd->tcd_image) {
1863 return;
1864 }
1865
1866 if (p_tcd->m_is_decoder) {
1867 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1868 } else {
1869 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1870 }
1871
1872 l_tile = p_tcd->tcd_image->tiles;
1873 if (! l_tile) {
1874 return;
1875 }
1876
1877 l_tile_comp = l_tile->comps;
1878
1879 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1880 l_res = l_tile_comp->resolutions;
1881 if (l_res) {
1882
1883 l_nb_resolutions = l_tile_comp->resolutions_size / (OPJ_UINT32)sizeof(
1884 opj_tcd_resolution_t);
1885 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1886 l_band = l_res->bands;
1887 for (bandno = 0; bandno < 3; ++bandno) {
1888 l_precinct = l_band->precincts;
1889 if (l_precinct) {
1890
1891 l_nb_precincts = l_band->precincts_data_size / (OPJ_UINT32)sizeof(
1892 opj_tcd_precinct_t);
1893 for (precno = 0; precno < l_nb_precincts; ++precno) {
1894 opj_tgt_destroy(l_precinct->incltree);
1895 l_precinct->incltree = 00;
1896 opj_tgt_destroy(l_precinct->imsbtree);
1897 l_precinct->imsbtree = 00;
1898 (*l_tcd_code_block_deallocate)(l_precinct);
1899 ++l_precinct;
1900 }
1901
1902 opj_free(l_band->precincts);
1903 l_band->precincts = 00;
1904 }
1905 ++l_band;
1906 } /* for (resno */
1907 ++l_res;
1908 }
1909
1910 opj_free(l_tile_comp->resolutions);
1911 l_tile_comp->resolutions = 00;
1912 }
1913
1914 if (l_tile_comp->ownsData && l_tile_comp->data) {
1915 opj_image_data_free(l_tile_comp->data);
1916 l_tile_comp->data = 00;
1917 l_tile_comp->ownsData = 0;
1918 l_tile_comp->data_size = 0;
1919 l_tile_comp->data_size_needed = 0;
1920 }
1921
1922 opj_image_data_free(l_tile_comp->data_win);
1923
1924 ++l_tile_comp;
1925 }
1926
1927 opj_free(l_tile->comps);
1928 l_tile->comps = 00;
1929 opj_free(p_tcd->tcd_image->tiles);
1930 p_tcd->tcd_image->tiles = 00;
1931 }
1932
1933
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)1934 static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1935 OPJ_BYTE * p_src_data,
1936 OPJ_UINT32 * p_data_read,
1937 OPJ_UINT32 p_max_src_size,
1938 opj_codestream_index_t *p_cstr_index,
1939 opj_event_mgr_t *p_manager
1940 )
1941 {
1942 opj_t2_t * l_t2;
1943
1944 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1945 if (l_t2 == 00) {
1946 return OPJ_FALSE;
1947 }
1948
1949 if (! opj_t2_decode_packets(
1950 p_tcd,
1951 l_t2,
1952 p_tcd->tcd_tileno,
1953 p_tcd->tcd_image->tiles,
1954 p_src_data,
1955 p_data_read,
1956 p_max_src_size,
1957 p_cstr_index,
1958 p_manager)) {
1959 opj_t2_destroy(l_t2);
1960 return OPJ_FALSE;
1961 }
1962
1963 opj_t2_destroy(l_t2);
1964
1965 /*---------------CLEAN-------------------*/
1966 return OPJ_TRUE;
1967 }
1968
opj_tcd_t1_decode(opj_tcd_t * p_tcd,opj_event_mgr_t * p_manager)1969 static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1970 {
1971 OPJ_UINT32 compno;
1972 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1973 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1974 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1975 volatile OPJ_BOOL ret = OPJ_TRUE;
1976 OPJ_BOOL check_pterm = OPJ_FALSE;
1977 opj_mutex_t* p_manager_mutex = NULL;
1978
1979 p_manager_mutex = opj_mutex_create();
1980
1981 /* Only enable PTERM check if we decode all layers */
1982 if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
1983 (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
1984 check_pterm = OPJ_TRUE;
1985 }
1986
1987 for (compno = 0; compno < l_tile->numcomps;
1988 ++compno, ++l_tile_comp, ++l_tccp) {
1989 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1990 continue;
1991 }
1992
1993 opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp,
1994 p_manager, p_manager_mutex, check_pterm);
1995 if (!ret) {
1996 break;
1997 }
1998 }
1999
2000 opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
2001 if (p_manager_mutex) {
2002 opj_mutex_destroy(p_manager_mutex);
2003 }
2004 return ret;
2005 }
2006
2007
opj_tcd_dwt_decode(opj_tcd_t * p_tcd)2008 static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
2009 {
2010 OPJ_UINT32 compno;
2011 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2012 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2013 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2014 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2015
2016 for (compno = 0; compno < l_tile->numcomps;
2017 compno++, ++l_tile_comp, ++l_img_comp, ++l_tccp) {
2018 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2019 continue;
2020 }
2021
2022 if (l_tccp->qmfbid == 1) {
2023 if (! opj_dwt_decode(p_tcd, l_tile_comp,
2024 l_img_comp->resno_decoded + 1)) {
2025 return OPJ_FALSE;
2026 }
2027 } else {
2028 if (! opj_dwt_decode_real(p_tcd, l_tile_comp,
2029 l_img_comp->resno_decoded + 1)) {
2030 return OPJ_FALSE;
2031 }
2032 }
2033
2034 }
2035
2036 return OPJ_TRUE;
2037 }
2038
opj_tcd_mct_decode(opj_tcd_t * p_tcd,opj_event_mgr_t * p_manager)2039 static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
2040 {
2041 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2042 opj_tcp_t * l_tcp = p_tcd->tcp;
2043 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2044 OPJ_UINT32 l_samples, i;
2045
2046 if (l_tcp->mct == 0 || p_tcd->used_component != NULL) {
2047 return OPJ_TRUE;
2048 }
2049
2050 if (p_tcd->whole_tile_decoding) {
2051 opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2052 l_tile_comp->minimum_num_resolutions - 1;
2053
2054 /* A bit inefficient: we process more data than needed if */
2055 /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */
2056 /* but we would need to take into account a stride then */
2057 l_samples = (OPJ_UINT32)((res_comp0->x1 - res_comp0->x0) *
2058 (res_comp0->y1 - res_comp0->y0));
2059 if (l_tile->numcomps >= 3) {
2060 if (l_tile_comp->minimum_num_resolutions !=
2061 l_tile->comps[1].minimum_num_resolutions ||
2062 l_tile_comp->minimum_num_resolutions !=
2063 l_tile->comps[2].minimum_num_resolutions) {
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 if (l_tile->numcomps >= 3) {
2070 opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2071 l_tile_comp->minimum_num_resolutions - 1;
2072 opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2073 l_tile_comp->minimum_num_resolutions - 1;
2074 /* testcase 1336.pdf.asan.47.376 */
2075 if (p_tcd->image->comps[0].resno_decoded !=
2076 p_tcd->image->comps[1].resno_decoded ||
2077 p_tcd->image->comps[0].resno_decoded !=
2078 p_tcd->image->comps[2].resno_decoded ||
2079 (OPJ_SIZE_T)(res_comp1->x1 - res_comp1->x0) *
2080 (OPJ_SIZE_T)(res_comp1->y1 - res_comp1->y0) != l_samples ||
2081 (OPJ_SIZE_T)(res_comp2->x1 - res_comp2->x0) *
2082 (OPJ_SIZE_T)(res_comp2->y1 - res_comp2->y0) != l_samples) {
2083 opj_event_msg(p_manager, EVT_ERROR,
2084 "Tiles don't all have the same dimension. Skip the MCT step.\n");
2085 return OPJ_FALSE;
2086 }
2087 }
2088 } else {
2089 opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2090 p_tcd->image->comps[0].resno_decoded;
2091
2092 l_samples = (res_comp0->win_x1 - res_comp0->win_x0) *
2093 (res_comp0->win_y1 - res_comp0->win_y0);
2094 if (l_tile->numcomps >= 3) {
2095 opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2096 p_tcd->image->comps[1].resno_decoded;
2097 opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2098 p_tcd->image->comps[2].resno_decoded;
2099 /* testcase 1336.pdf.asan.47.376 */
2100 if (p_tcd->image->comps[0].resno_decoded !=
2101 p_tcd->image->comps[1].resno_decoded ||
2102 p_tcd->image->comps[0].resno_decoded !=
2103 p_tcd->image->comps[2].resno_decoded ||
2104 (OPJ_SIZE_T)(res_comp1->win_x1 - res_comp1->win_x0) *
2105 (OPJ_SIZE_T)(res_comp1->win_y1 - res_comp1->win_y0) != l_samples ||
2106 (OPJ_SIZE_T)(res_comp2->win_x1 - res_comp2->win_x0) *
2107 (OPJ_SIZE_T)(res_comp2->win_y1 - res_comp2->win_y0) != l_samples) {
2108 opj_event_msg(p_manager, EVT_ERROR,
2109 "Tiles don't all have the same dimension. Skip the MCT step.\n");
2110 return OPJ_FALSE;
2111 }
2112 }
2113 }
2114
2115 if (l_tile->numcomps >= 3) {
2116 if (l_tcp->mct == 2) {
2117 OPJ_BYTE ** l_data;
2118
2119 if (! l_tcp->m_mct_decoding_matrix) {
2120 return OPJ_TRUE;
2121 }
2122
2123 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2124 if (! l_data) {
2125 return OPJ_FALSE;
2126 }
2127
2128 for (i = 0; i < l_tile->numcomps; ++i) {
2129 if (p_tcd->whole_tile_decoding) {
2130 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2131 } else {
2132 l_data[i] = (OPJ_BYTE*) l_tile_comp->data_win;
2133 }
2134 ++l_tile_comp;
2135 }
2136
2137 if (! opj_mct_decode_custom(/* MCT data */
2138 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
2139 /* size of components */
2140 l_samples,
2141 /* components */
2142 l_data,
2143 /* nb of components (i.e. size of pData) */
2144 l_tile->numcomps,
2145 /* tells if the data is signed */
2146 p_tcd->image->comps->sgnd)) {
2147 opj_free(l_data);
2148 return OPJ_FALSE;
2149 }
2150
2151 opj_free(l_data);
2152 } else {
2153 if (l_tcp->tccps->qmfbid == 1) {
2154 if (p_tcd->whole_tile_decoding) {
2155 opj_mct_decode(l_tile->comps[0].data,
2156 l_tile->comps[1].data,
2157 l_tile->comps[2].data,
2158 l_samples);
2159 } else {
2160 opj_mct_decode(l_tile->comps[0].data_win,
2161 l_tile->comps[1].data_win,
2162 l_tile->comps[2].data_win,
2163 l_samples);
2164 }
2165 } else {
2166 if (p_tcd->whole_tile_decoding) {
2167 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
2168 (OPJ_FLOAT32*)l_tile->comps[1].data,
2169 (OPJ_FLOAT32*)l_tile->comps[2].data,
2170 l_samples);
2171 } else {
2172 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data_win,
2173 (OPJ_FLOAT32*)l_tile->comps[1].data_win,
2174 (OPJ_FLOAT32*)l_tile->comps[2].data_win,
2175 l_samples);
2176 }
2177 }
2178 }
2179 } else {
2180 opj_event_msg(p_manager, EVT_ERROR,
2181 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
2182 l_tile->numcomps);
2183 }
2184
2185 return OPJ_TRUE;
2186 }
2187
2188
opj_tcd_dc_level_shift_decode(opj_tcd_t * p_tcd)2189 static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
2190 {
2191 OPJ_UINT32 compno;
2192 opj_tcd_tilecomp_t * l_tile_comp = 00;
2193 opj_tccp_t * l_tccp = 00;
2194 opj_image_comp_t * l_img_comp = 00;
2195 opj_tcd_resolution_t* l_res = 00;
2196 opj_tcd_tile_t * l_tile;
2197 OPJ_UINT32 l_width, l_height, i, j;
2198 OPJ_INT32 * l_current_ptr;
2199 OPJ_INT32 l_min, l_max;
2200 OPJ_UINT32 l_stride;
2201
2202 l_tile = p_tcd->tcd_image->tiles;
2203 l_tile_comp = l_tile->comps;
2204 l_tccp = p_tcd->tcp->tccps;
2205 l_img_comp = p_tcd->image->comps;
2206
2207 for (compno = 0; compno < l_tile->numcomps;
2208 compno++, ++l_img_comp, ++l_tccp, ++l_tile_comp) {
2209
2210 if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2211 continue;
2212 }
2213
2214 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
2215
2216 if (!p_tcd->whole_tile_decoding) {
2217 l_width = l_res->win_x1 - l_res->win_x0;
2218 l_height = l_res->win_y1 - l_res->win_y0;
2219 l_stride = 0;
2220 l_current_ptr = l_tile_comp->data_win;
2221 } else {
2222 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
2223 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
2224 l_stride = (OPJ_UINT32)(
2225 l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x1 -
2226 l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x0)
2227 - l_width;
2228 l_current_ptr = l_tile_comp->data;
2229
2230 assert(l_height == 0 ||
2231 l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
2232 }
2233
2234 if (l_img_comp->sgnd) {
2235 l_min = -(1 << (l_img_comp->prec - 1));
2236 l_max = (1 << (l_img_comp->prec - 1)) - 1;
2237 } else {
2238 l_min = 0;
2239 l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
2240 }
2241
2242
2243 if (l_tccp->qmfbid == 1) {
2244 for (j = 0; j < l_height; ++j) {
2245 for (i = 0; i < l_width; ++i) {
2246 /* TODO: do addition on int64 ? */
2247 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
2248 l_max);
2249 ++l_current_ptr;
2250 }
2251 l_current_ptr += l_stride;
2252 }
2253 } else {
2254 for (j = 0; j < l_height; ++j) {
2255 for (i = 0; i < l_width; ++i) {
2256 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
2257 if (l_value > INT_MAX) {
2258 *l_current_ptr = l_max;
2259 } else if (l_value < INT_MIN) {
2260 *l_current_ptr = l_min;
2261 } else {
2262 /* Do addition on int64 to avoid overflows */
2263 OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value);
2264 *l_current_ptr = (OPJ_INT32)opj_int64_clamp(
2265 l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
2266 }
2267 ++l_current_ptr;
2268 }
2269 l_current_ptr += l_stride;
2270 }
2271 }
2272 }
2273
2274 return OPJ_TRUE;
2275 }
2276
2277
2278
2279 /**
2280 * Deallocates the encoding data of the given precinct.
2281 */
opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)2282 static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
2283 {
2284 OPJ_UINT32 cblkno, l_nb_code_blocks;
2285
2286 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
2287 if (l_code_block) {
2288 /*fprintf(stderr,"deallocate codeblock:{\n");*/
2289 /*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);*/
2290 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
2291 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 );*/
2292
2293
2294 l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2295 opj_tcd_cblk_dec_t);
2296 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
2297
2298 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2299
2300 if (l_code_block->segs) {
2301 opj_free(l_code_block->segs);
2302 l_code_block->segs = 00;
2303 }
2304
2305 if (l_code_block->chunks) {
2306 opj_free(l_code_block->chunks);
2307 l_code_block->chunks = 00;
2308 }
2309
2310 opj_aligned_free(l_code_block->decoded_data);
2311 l_code_block->decoded_data = NULL;
2312
2313 ++l_code_block;
2314 }
2315
2316 opj_free(p_precinct->cblks.dec);
2317 p_precinct->cblks.dec = 00;
2318 }
2319 }
2320
2321 /**
2322 * Deallocates the encoding data of the given precinct.
2323 */
opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)2324 static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
2325 {
2326 OPJ_UINT32 cblkno, l_nb_code_blocks;
2327
2328 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
2329 if (l_code_block) {
2330 l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2331 opj_tcd_cblk_enc_t);
2332
2333 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2334 if (l_code_block->data) {
2335 /* We refer to data - 1 since below we incremented it */
2336 /* in opj_tcd_code_block_enc_allocate_data() */
2337 opj_free(l_code_block->data - 1);
2338 l_code_block->data = 00;
2339 }
2340
2341 if (l_code_block->layers) {
2342 opj_free(l_code_block->layers);
2343 l_code_block->layers = 00;
2344 }
2345
2346 if (l_code_block->passes) {
2347 opj_free(l_code_block->passes);
2348 l_code_block->passes = 00;
2349 }
2350 ++l_code_block;
2351 }
2352
2353 opj_free(p_precinct->cblks.enc);
2354
2355 p_precinct->cblks.enc = 00;
2356 }
2357 }
2358
opj_tcd_get_encoded_tile_size(opj_tcd_t * p_tcd)2359 OPJ_SIZE_T opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd)
2360 {
2361 OPJ_UINT32 i;
2362 OPJ_SIZE_T l_data_size = 0;
2363 opj_image_comp_t * l_img_comp = 00;
2364 opj_tcd_tilecomp_t * l_tilec = 00;
2365 OPJ_UINT32 l_size_comp, l_remaining;
2366
2367 l_tilec = p_tcd->tcd_image->tiles->comps;
2368 l_img_comp = p_tcd->image->comps;
2369 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2370 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2371 l_remaining = l_img_comp->prec & 7; /* (%8) */
2372
2373 if (l_remaining) {
2374 ++l_size_comp;
2375 }
2376
2377 if (l_size_comp == 3) {
2378 l_size_comp = 4;
2379 }
2380
2381 l_data_size += l_size_comp * ((OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2382 (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0));
2383 ++l_img_comp;
2384 ++l_tilec;
2385 }
2386
2387 return l_data_size;
2388 }
2389
opj_tcd_dc_level_shift_encode(opj_tcd_t * p_tcd)2390 static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2391 {
2392 OPJ_UINT32 compno;
2393 opj_tcd_tilecomp_t * l_tile_comp = 00;
2394 opj_tccp_t * l_tccp = 00;
2395 opj_image_comp_t * l_img_comp = 00;
2396 opj_tcd_tile_t * l_tile;
2397 OPJ_SIZE_T l_nb_elem, i;
2398 OPJ_INT32 * l_current_ptr;
2399
2400 l_tile = p_tcd->tcd_image->tiles;
2401 l_tile_comp = l_tile->comps;
2402 l_tccp = p_tcd->tcp->tccps;
2403 l_img_comp = p_tcd->image->comps;
2404
2405 for (compno = 0; compno < l_tile->numcomps; compno++) {
2406 l_current_ptr = l_tile_comp->data;
2407 l_nb_elem = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2408 (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2409
2410 if (l_tccp->qmfbid == 1) {
2411 for (i = 0; i < l_nb_elem; ++i) {
2412 *l_current_ptr -= l_tccp->m_dc_level_shift ;
2413 ++l_current_ptr;
2414 }
2415 } else {
2416 for (i = 0; i < l_nb_elem; ++i) {
2417 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
2418 ++l_current_ptr;
2419 }
2420 }
2421
2422 ++l_img_comp;
2423 ++l_tccp;
2424 ++l_tile_comp;
2425 }
2426
2427 return OPJ_TRUE;
2428 }
2429
opj_tcd_mct_encode(opj_tcd_t * p_tcd)2430 static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2431 {
2432 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2433 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2434 OPJ_SIZE_T samples = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2435 (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2436 OPJ_UINT32 i;
2437 OPJ_BYTE ** l_data = 00;
2438 opj_tcp_t * l_tcp = p_tcd->tcp;
2439
2440 if (!p_tcd->tcp->mct) {
2441 return OPJ_TRUE;
2442 }
2443
2444 if (p_tcd->tcp->mct == 2) {
2445 if (! p_tcd->tcp->m_mct_coding_matrix) {
2446 return OPJ_TRUE;
2447 }
2448
2449 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2450 if (! l_data) {
2451 return OPJ_FALSE;
2452 }
2453
2454 for (i = 0; i < l_tile->numcomps; ++i) {
2455 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2456 ++l_tile_comp;
2457 }
2458
2459 if (! opj_mct_encode_custom(/* MCT data */
2460 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2461 /* size of components */
2462 samples,
2463 /* components */
2464 l_data,
2465 /* nb of components (i.e. size of pData) */
2466 l_tile->numcomps,
2467 /* tells if the data is signed */
2468 p_tcd->image->comps->sgnd)) {
2469 opj_free(l_data);
2470 return OPJ_FALSE;
2471 }
2472
2473 opj_free(l_data);
2474 } else if (l_tcp->tccps->qmfbid == 0) {
2475 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data,
2476 l_tile->comps[2].data, samples);
2477 } else {
2478 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2479 l_tile->comps[2].data, samples);
2480 }
2481
2482 return OPJ_TRUE;
2483 }
2484
opj_tcd_dwt_encode(opj_tcd_t * p_tcd)2485 static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2486 {
2487 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2488 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2489 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2490 OPJ_UINT32 compno;
2491
2492 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2493 if (l_tccp->qmfbid == 1) {
2494 if (! opj_dwt_encode(l_tile_comp)) {
2495 return OPJ_FALSE;
2496 }
2497 } else if (l_tccp->qmfbid == 0) {
2498 if (! opj_dwt_encode_real(l_tile_comp)) {
2499 return OPJ_FALSE;
2500 }
2501 }
2502
2503 ++l_tile_comp;
2504 ++l_tccp;
2505 }
2506
2507 return OPJ_TRUE;
2508 }
2509
opj_tcd_t1_encode(opj_tcd_t * p_tcd)2510 static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2511 {
2512 opj_t1_t * l_t1;
2513 const OPJ_FLOAT64 * l_mct_norms;
2514 OPJ_UINT32 l_mct_numcomps = 0U;
2515 opj_tcp_t * l_tcp = p_tcd->tcp;
2516
2517 l_t1 = opj_t1_create(OPJ_TRUE);
2518 if (l_t1 == 00) {
2519 return OPJ_FALSE;
2520 }
2521
2522 if (l_tcp->mct == 1) {
2523 l_mct_numcomps = 3U;
2524 /* irreversible encoding */
2525 if (l_tcp->tccps->qmfbid == 0) {
2526 l_mct_norms = opj_mct_get_mct_norms_real();
2527 } else {
2528 l_mct_norms = opj_mct_get_mct_norms();
2529 }
2530 } else {
2531 l_mct_numcomps = p_tcd->image->numcomps;
2532 l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2533 }
2534
2535 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2536 l_mct_numcomps)) {
2537 opj_t1_destroy(l_t1);
2538 return OPJ_FALSE;
2539 }
2540
2541 opj_t1_destroy(l_t1);
2542
2543 return OPJ_TRUE;
2544 }
2545
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)2546 static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2547 OPJ_BYTE * p_dest_data,
2548 OPJ_UINT32 * p_data_written,
2549 OPJ_UINT32 p_max_dest_size,
2550 opj_codestream_info_t *p_cstr_info,
2551 opj_event_mgr_t *p_manager)
2552 {
2553 opj_t2_t * l_t2;
2554
2555 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2556 if (l_t2 == 00) {
2557 return OPJ_FALSE;
2558 }
2559
2560 if (! opj_t2_encode_packets(
2561 l_t2,
2562 p_tcd->tcd_tileno,
2563 p_tcd->tcd_image->tiles,
2564 p_tcd->tcp->numlayers,
2565 p_dest_data,
2566 p_data_written,
2567 p_max_dest_size,
2568 p_cstr_info,
2569 p_tcd->tp_num,
2570 p_tcd->tp_pos,
2571 p_tcd->cur_pino,
2572 FINAL_PASS,
2573 p_manager)) {
2574 opj_t2_destroy(l_t2);
2575 return OPJ_FALSE;
2576 }
2577
2578 opj_t2_destroy(l_t2);
2579
2580 /*---------------CLEAN-------------------*/
2581 return OPJ_TRUE;
2582 }
2583
2584
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)2585 static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2586 OPJ_BYTE * p_dest_data,
2587 OPJ_UINT32 p_max_dest_size,
2588 opj_codestream_info_t *p_cstr_info,
2589 opj_event_mgr_t *p_manager)
2590 {
2591 opj_cp_t * l_cp = p_tcd->cp;
2592 OPJ_UINT32 l_nb_written = 0;
2593
2594 if (p_cstr_info) {
2595 p_cstr_info->index_write = 0;
2596 }
2597
2598 if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2599 l_cp->m_specific_param.m_enc.m_fixed_quality) {
2600 /* fixed_quality */
2601 /* Normal Rate/distortion allocation */
2602 if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2603 p_cstr_info, p_manager)) {
2604 return OPJ_FALSE;
2605 }
2606 } else {
2607 /* Fixed layer allocation */
2608 opj_tcd_rateallocate_fixed(p_tcd);
2609 }
2610
2611 return OPJ_TRUE;
2612 }
2613
2614
opj_tcd_copy_tile_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_src,OPJ_SIZE_T p_src_length)2615 OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2616 OPJ_BYTE * p_src,
2617 OPJ_SIZE_T p_src_length)
2618 {
2619 OPJ_UINT32 i;
2620 OPJ_SIZE_T j;
2621 OPJ_SIZE_T l_data_size = 0;
2622 opj_image_comp_t * l_img_comp = 00;
2623 opj_tcd_tilecomp_t * l_tilec = 00;
2624 OPJ_UINT32 l_size_comp, l_remaining;
2625 OPJ_SIZE_T l_nb_elem;
2626
2627 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2628 if (l_data_size != p_src_length) {
2629 return OPJ_FALSE;
2630 }
2631
2632 l_tilec = p_tcd->tcd_image->tiles->comps;
2633 l_img_comp = p_tcd->image->comps;
2634 for (i = 0; i < p_tcd->image->numcomps; ++i) {
2635 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2636 l_remaining = l_img_comp->prec & 7; /* (%8) */
2637 l_nb_elem = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2638 (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
2639
2640 if (l_remaining) {
2641 ++l_size_comp;
2642 }
2643
2644 if (l_size_comp == 3) {
2645 l_size_comp = 4;
2646 }
2647
2648 switch (l_size_comp) {
2649 case 1: {
2650 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2651 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2652
2653 if (l_img_comp->sgnd) {
2654 for (j = 0; j < l_nb_elem; ++j) {
2655 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2656 }
2657 } else {
2658 for (j = 0; j < l_nb_elem; ++j) {
2659 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2660 }
2661 }
2662
2663 p_src = (OPJ_BYTE*) l_src_ptr;
2664 }
2665 break;
2666 case 2: {
2667 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2668 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2669
2670 if (l_img_comp->sgnd) {
2671 for (j = 0; j < l_nb_elem; ++j) {
2672 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2673 }
2674 } else {
2675 for (j = 0; j < l_nb_elem; ++j) {
2676 *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2677 }
2678 }
2679
2680 p_src = (OPJ_BYTE*) l_src_ptr;
2681 }
2682 break;
2683 case 4: {
2684 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2685 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2686
2687 for (j = 0; j < l_nb_elem; ++j) {
2688 *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2689 }
2690
2691 p_src = (OPJ_BYTE*) l_src_ptr;
2692 }
2693 break;
2694 }
2695
2696 ++l_img_comp;
2697 ++l_tilec;
2698 }
2699
2700 return OPJ_TRUE;
2701 }
2702
opj_tcd_is_band_empty(opj_tcd_band_t * band)2703 OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2704 {
2705 return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);
2706 }
2707
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)2708 OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd,
2709 OPJ_UINT32 compno,
2710 OPJ_UINT32 resno,
2711 OPJ_UINT32 bandno,
2712 OPJ_UINT32 band_x0,
2713 OPJ_UINT32 band_y0,
2714 OPJ_UINT32 band_x1,
2715 OPJ_UINT32 band_y1)
2716 {
2717 /* Note: those values for filter_margin are in part the result of */
2718 /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
2719 /* to the maximum left/right extension given in tables F.2 and F.3 of the */
2720 /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
2721 /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
2722 /* needed to be bumped to 4, in case inconsistencies are found while */
2723 /* decoding parts of irreversible coded images. */
2724 /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
2725 OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3;
2726 opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]);
2727 opj_image_comp_t* image_comp = &(tcd->image->comps[compno]);
2728 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2729 /* with the tile coordinates */
2730 OPJ_UINT32 tcx0 = opj_uint_max(
2731 (OPJ_UINT32)tilec->x0,
2732 opj_uint_ceildiv(tcd->win_x0, image_comp->dx));
2733 OPJ_UINT32 tcy0 = opj_uint_max(
2734 (OPJ_UINT32)tilec->y0,
2735 opj_uint_ceildiv(tcd->win_y0, image_comp->dy));
2736 OPJ_UINT32 tcx1 = opj_uint_min(
2737 (OPJ_UINT32)tilec->x1,
2738 opj_uint_ceildiv(tcd->win_x1, image_comp->dx));
2739 OPJ_UINT32 tcy1 = opj_uint_min(
2740 (OPJ_UINT32)tilec->y1,
2741 opj_uint_ceildiv(tcd->win_y1, image_comp->dy));
2742 /* Compute number of decomposition for this band. See table F-1 */
2743 OPJ_UINT32 nb = (resno == 0) ?
2744 tilec->numresolutions - 1 :
2745 tilec->numresolutions - resno;
2746 /* Map above tile-based coordinates to sub-band-based coordinates per */
2747 /* equation B-15 of the standard */
2748 OPJ_UINT32 x0b = bandno & 1;
2749 OPJ_UINT32 y0b = bandno >> 1;
2750 OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 :
2751 (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2752 opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2753 OPJ_UINT32 tby0 = (nb == 0) ? tcy0 :
2754 (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2755 opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2756 OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 :
2757 (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2758 opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2759 OPJ_UINT32 tby1 = (nb == 0) ? tcy1 :
2760 (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2761 opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2762 OPJ_BOOL intersects;
2763
2764 if (tbx0 < filter_margin) {
2765 tbx0 = 0;
2766 } else {
2767 tbx0 -= filter_margin;
2768 }
2769 if (tby0 < filter_margin) {
2770 tby0 = 0;
2771 } else {
2772 tby0 -= filter_margin;
2773 }
2774 tbx1 = opj_uint_adds(tbx1, filter_margin);
2775 tby1 = opj_uint_adds(tby1, filter_margin);
2776
2777 intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 &&
2778 band_y1 > tby0;
2779
2780 #ifdef DEBUG_VERBOSE
2781 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",
2782 compno, resno, nb, bandno, x0b, y0b,
2783 band_x0, band_y0, band_x1, band_y1,
2784 tbx0, tby0, tbx1, tby1, intersects);
2785 #endif
2786 return intersects;
2787 }
2788
2789 /** Returns whether a tile componenent is fully decoded, taking into account
2790 * p_tcd->win_* members.
2791 *
2792 * @param p_tcd TCD handle.
2793 * @param compno Component number
2794 * @return OPJ_TRUE whether the tile componenent is fully decoded
2795 */
opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t * p_tcd,OPJ_UINT32 compno)2796 static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *p_tcd,
2797 OPJ_UINT32 compno)
2798 {
2799 opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
2800 opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
2801 /* Compute the intersection of the area of interest, expressed in tile coordinates */
2802 /* with the tile coordinates */
2803 OPJ_UINT32 tcx0 = opj_uint_max(
2804 (OPJ_UINT32)tilec->x0,
2805 opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
2806 OPJ_UINT32 tcy0 = opj_uint_max(
2807 (OPJ_UINT32)tilec->y0,
2808 opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
2809 OPJ_UINT32 tcx1 = opj_uint_min(
2810 (OPJ_UINT32)tilec->x1,
2811 opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
2812 OPJ_UINT32 tcy1 = opj_uint_min(
2813 (OPJ_UINT32)tilec->y1,
2814 opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
2815
2816 OPJ_UINT32 shift = tilec->numresolutions - tilec->minimum_num_resolutions;
2817 /* Tolerate small margin within the reduced resolution factor to consider if */
2818 /* the whole tile path must be taken */
2819 return (tcx0 >= (OPJ_UINT32)tilec->x0 &&
2820 tcy0 >= (OPJ_UINT32)tilec->y0 &&
2821 tcx1 <= (OPJ_UINT32)tilec->x1 &&
2822 tcy1 <= (OPJ_UINT32)tilec->y1 &&
2823 (shift >= 32 ||
2824 (((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 &&
2825 ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 &&
2826 (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 &&
2827 (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0)));
2828 }
2829