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