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.
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 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_includes.h"
41 /** @defgroup PI PI - Implementation of a packet iterator */
44 /** @name Local static functions */
48 Get next packet in layer-resolution-component-precinct order.
49 @param pi packet iterator to modify
50 @return returns false if pi pointed to the last packet or else returns true
52 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
54 Get next packet in resolution-layer-component-precinct order.
55 @param pi packet iterator to modify
56 @return returns false if pi pointed to the last packet or else returns true
58 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
60 Get next packet in resolution-precinct-component-layer order.
61 @param pi packet iterator to modify
62 @return returns false if pi pointed to the last packet or else returns true
64 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
66 Get next packet in precinct-component-resolution-layer order.
67 @param pi packet iterator to modify
68 @return returns false if pi pointed to the last packet or else returns true
70 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
72 Get next packet in component-precinct-resolution-layer order.
73 @param pi packet iterator to modify
74 @return returns false if pi pointed to the last packet or else returns true
76 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
79 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
81 * @param p_cp the coding parameters to modify
82 * @param p_tileno the tile index being concerned.
83 * @param p_tx0 X0 parameter for the tile
84 * @param p_tx1 X1 parameter for the tile
85 * @param p_ty0 Y0 parameter for the tile
86 * @param p_ty1 Y1 parameter for the tile
87 * @param p_max_prec the maximum precision for all the bands of the tile
88 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
89 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
90 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
92 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
98 OPJ_UINT32 p_max_prec,
101 OPJ_UINT32 p_dy_min);
104 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
106 * @param p_cp the coding parameters to modify
107 * @param p_num_comps the number of components
108 * @param p_tileno the tile index being concerned.
109 * @param p_tx0 X0 parameter for the tile
110 * @param p_tx1 X1 parameter for the tile
111 * @param p_ty0 Y0 parameter for the tile
112 * @param p_ty1 Y1 parameter for the tile
113 * @param p_max_prec the maximum precision for all the bands of the tile
114 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
115 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
116 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
118 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
119 OPJ_UINT32 p_num_comps,
125 OPJ_UINT32 p_max_prec,
126 OPJ_UINT32 p_max_res,
128 OPJ_UINT32 p_dy_min);
130 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
132 * @param p_image the image being encoded.
133 * @param p_cp the coding parameters.
134 * @param tileno the tile index of the tile being encoded.
135 * @param p_tx0 pointer that will hold the X0 parameter for the tile
136 * @param p_tx1 pointer that will hold the X1 parameter for the tile
137 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
138 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
139 * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
140 * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
141 * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
142 * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
144 static void opj_get_encoding_parameters(const opj_image_t *p_image,
145 const opj_cp_t *p_cp,
151 OPJ_UINT32 * p_dx_min,
152 OPJ_UINT32 * p_dy_min,
153 OPJ_UINT32 * p_max_prec,
154 OPJ_UINT32 * p_max_res);
157 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
158 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
159 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
160 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
161 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
163 * @param p_image the image being encoded.
164 * @param p_cp the coding parameters.
165 * @param tileno the tile index of the tile being encoded.
166 * @param p_tx0 pointer that will hold the X0 parameter for the tile
167 * @param p_tx1 pointer that will hold the X1 parameter for the tile
168 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
169 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
170 * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
171 * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
172 * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
173 * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
174 * @param p_resolutions pointer to an area corresponding to the one described above.
176 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
177 const opj_cp_t *p_cp,
183 OPJ_UINT32 * p_dx_min,
184 OPJ_UINT32 * p_dy_min,
185 OPJ_UINT32 * p_max_prec,
186 OPJ_UINT32 * p_max_res,
187 OPJ_UINT32 ** p_resolutions);
189 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
190 * No other data is set. The include section of the packet iterator is not allocated.
192 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
193 * @param p_cp the coding parameters.
194 * @param tileno the index of the tile from which creating the packet iterator.
196 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
197 const opj_cp_t *p_cp,
202 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
204 OPJ_UINT32 p_max_precision,
205 OPJ_UINT32 p_max_res);
209 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
211 OPJ_UINT32 p_max_precision,
212 OPJ_UINT32 p_max_res);
217 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
221 const OPJ_CHAR *prog);
228 ==========================================================
230 ==========================================================
233 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
235 opj_pi_comp_t *comp = NULL;
236 opj_pi_resolution_t *res = NULL;
237 OPJ_UINT32 index = 0;
240 comp = &pi->comps[pi->compno];
241 res = &comp->resolutions[pi->resno];
247 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
248 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
250 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
251 comp = &pi->comps[pi->compno];
252 if (pi->resno >= comp->numresolutions) {
255 res = &comp->resolutions[pi->resno];
257 pi->poc.precno1 = res->pw * res->ph;
259 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
260 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
261 pi->step_c + pi->precno * pi->step_p;
262 if (!pi->include[index]) {
263 pi->include[index] = 1;
276 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
278 opj_pi_comp_t *comp = NULL;
279 opj_pi_resolution_t *res = NULL;
280 OPJ_UINT32 index = 0;
283 comp = &pi->comps[pi->compno];
284 res = &comp->resolutions[pi->resno];
290 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
291 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
292 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
293 comp = &pi->comps[pi->compno];
294 if (pi->resno >= comp->numresolutions) {
297 res = &comp->resolutions[pi->resno];
299 pi->poc.precno1 = res->pw * res->ph;
301 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
302 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
303 pi->step_c + pi->precno * pi->step_p;
304 if (!pi->include[index]) {
305 pi->include[index] = 1;
318 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
320 opj_pi_comp_t *comp = NULL;
321 opj_pi_resolution_t *res = NULL;
322 OPJ_UINT32 index = 0;
327 OPJ_UINT32 compno, resno;
331 for (compno = 0; compno < pi->numcomps; compno++) {
332 comp = &pi->comps[compno];
333 for (resno = 0; resno < comp->numresolutions; resno++) {
335 res = &comp->resolutions[resno];
336 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
337 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
338 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
339 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
344 pi->poc.ty0 = pi->ty0;
345 pi->poc.tx0 = pi->tx0;
346 pi->poc.ty1 = pi->ty1;
347 pi->poc.tx1 = pi->tx1;
349 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
350 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
351 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
352 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
353 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
354 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
356 OPJ_INT32 trx0, try0;
357 OPJ_INT32 trx1, try1;
359 OPJ_INT32 prci, prcj;
360 comp = &pi->comps[pi->compno];
361 if (pi->resno >= comp->numresolutions) {
364 res = &comp->resolutions[pi->resno];
365 levelno = comp->numresolutions - 1 - pi->resno;
366 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
367 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
368 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
369 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
370 rpx = res->pdx + levelno;
371 rpy = res->pdy + levelno;
373 /* To avoid divisions by zero / undefined behaviour on shift */
375 /* Fixes reading id:000026,sig:08,src:002419,op:int32,pos:60,val:+32 */
376 /* of https://github.com/uclouvain/openjpeg/issues/938 */
377 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
378 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
382 /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
383 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
384 ((try0 << levelno) % (1 << rpy))))) {
387 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
388 ((trx0 << levelno) % (1 << rpx))))) {
392 if ((res->pw == 0) || (res->ph == 0)) {
396 if ((trx0 == trx1) || (try0 == try1)) {
400 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
401 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
402 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
403 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
404 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
405 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
406 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
407 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
408 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
409 pi->step_c + pi->precno * pi->step_p;
410 if (!pi->include[index]) {
411 pi->include[index] = 1;
425 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
427 opj_pi_comp_t *comp = NULL;
428 opj_pi_resolution_t *res = NULL;
429 OPJ_UINT32 index = 0;
432 comp = &pi->comps[pi->compno];
435 OPJ_UINT32 compno, resno;
439 for (compno = 0; compno < pi->numcomps; compno++) {
440 comp = &pi->comps[compno];
441 for (resno = 0; resno < comp->numresolutions; resno++) {
443 res = &comp->resolutions[resno];
444 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
445 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
446 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
447 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
452 pi->poc.ty0 = pi->ty0;
453 pi->poc.tx0 = pi->tx0;
454 pi->poc.ty1 = pi->ty1;
455 pi->poc.tx1 = pi->tx1;
457 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
458 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
459 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
460 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
461 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
462 comp = &pi->comps[pi->compno];
463 for (pi->resno = pi->poc.resno0;
464 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
466 OPJ_INT32 trx0, try0;
467 OPJ_INT32 trx1, try1;
469 OPJ_INT32 prci, prcj;
470 res = &comp->resolutions[pi->resno];
471 levelno = comp->numresolutions - 1 - pi->resno;
472 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
473 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
474 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
475 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
476 rpx = res->pdx + levelno;
477 rpy = res->pdy + levelno;
479 /* To avoid divisions by zero / undefined behaviour on shift */
481 /* Relates to id:000019,sig:08,src:001098,op:flip1,pos:49 */
482 /* of https://github.com/uclouvain/openjpeg/issues/938 */
483 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
484 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
488 /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
489 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
490 ((try0 << levelno) % (1 << rpy))))) {
493 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
494 ((trx0 << levelno) % (1 << rpx))))) {
498 if ((res->pw == 0) || (res->ph == 0)) {
502 if ((trx0 == trx1) || (try0 == try1)) {
506 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
507 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
508 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
509 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
510 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
511 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
512 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
513 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
514 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
515 pi->step_c + pi->precno * pi->step_p;
516 if (!pi->include[index]) {
517 pi->include[index] = 1;
531 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
533 opj_pi_comp_t *comp = NULL;
534 opj_pi_resolution_t *res = NULL;
535 OPJ_UINT32 index = 0;
538 comp = &pi->comps[pi->compno];
544 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
546 comp = &pi->comps[pi->compno];
549 for (resno = 0; resno < comp->numresolutions; resno++) {
551 res = &comp->resolutions[resno];
552 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
553 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
554 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
555 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
558 pi->poc.ty0 = pi->ty0;
559 pi->poc.tx0 = pi->tx0;
560 pi->poc.ty1 = pi->ty1;
561 pi->poc.tx1 = pi->tx1;
563 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
564 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
565 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
566 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
567 for (pi->resno = pi->poc.resno0;
568 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
570 OPJ_INT32 trx0, try0;
571 OPJ_INT32 trx1, try1;
573 OPJ_INT32 prci, prcj;
574 res = &comp->resolutions[pi->resno];
575 levelno = comp->numresolutions - 1 - pi->resno;
576 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
577 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
578 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
579 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
580 rpx = res->pdx + levelno;
581 rpy = res->pdy + levelno;
583 /* To avoid divisions by zero / undefined behaviour on shift */
585 /* Fixes reading id:000019,sig:08,src:001098,op:flip1,pos:49 */
586 /* of https://github.com/uclouvain/openjpeg/issues/938 */
587 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
588 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
592 /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
593 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
594 ((try0 << levelno) % (1 << rpy))))) {
597 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
598 ((trx0 << levelno) % (1 << rpx))))) {
602 if ((res->pw == 0) || (res->ph == 0)) {
606 if ((trx0 == trx1) || (try0 == try1)) {
610 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
611 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
612 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
613 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
614 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
615 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
616 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
617 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
618 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
619 pi->step_c + pi->precno * pi->step_p;
620 if (!pi->include[index]) {
621 pi->include[index] = 1;
635 static void opj_get_encoding_parameters(const opj_image_t *p_image,
636 const opj_cp_t *p_cp,
642 OPJ_UINT32 * p_dx_min,
643 OPJ_UINT32 * p_dy_min,
644 OPJ_UINT32 * p_max_prec,
645 OPJ_UINT32 * p_max_res)
648 OPJ_UINT32 compno, resno;
650 const opj_tcp_t *l_tcp = 00;
651 const opj_tccp_t * l_tccp = 00;
652 const opj_image_comp_t * l_img_comp = 00;
654 /* position in x and y of tile */
659 assert(p_image != 00);
660 assert(p_tileno < p_cp->tw * p_cp->th);
662 /* initializations */
663 l_tcp = &p_cp->tcps [p_tileno];
664 l_img_comp = p_image->comps;
665 l_tccp = l_tcp->tccps;
667 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
668 p = p_tileno % p_cp->tw;
669 q = p_tileno / p_cp->tw;
671 /* find extent of tile */
672 *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx),
673 (OPJ_INT32)p_image->x0);
674 *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx),
675 (OPJ_INT32)p_image->x1);
676 *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy),
677 (OPJ_INT32)p_image->y0);
678 *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy),
679 (OPJ_INT32)p_image->y1);
681 /* max precision is 0 (can only grow) */
685 /* take the largest value for dx_min and dy_min */
686 *p_dx_min = 0x7fffffff;
687 *p_dy_min = 0x7fffffff;
689 for (compno = 0; compno < p_image->numcomps; ++compno) {
690 /* arithmetic variables to calculate */
691 OPJ_UINT32 l_level_no;
692 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
693 OPJ_INT32 l_px0, l_py0, l_px1, py1;
694 OPJ_UINT32 l_pdx, l_pdy;
695 OPJ_UINT32 l_pw, l_ph;
696 OPJ_UINT32 l_product;
697 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
699 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
700 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
701 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
702 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
704 if (l_tccp->numresolutions > *p_max_res) {
705 *p_max_res = l_tccp->numresolutions;
708 /* use custom size for precincts */
709 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
710 OPJ_UINT32 l_dx, l_dy;
712 /* precinct width and height */
713 l_pdx = l_tccp->prcw[resno];
714 l_pdy = l_tccp->prch[resno];
716 l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
717 l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
719 /* take the minimum size for dx for each comp and resolution */
720 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
721 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
723 /* various calculations of extents */
724 l_level_no = l_tccp->numresolutions - 1 - resno;
726 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
727 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
728 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
729 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
731 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
732 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
733 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
735 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
737 l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
738 l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
740 l_product = l_pw * l_ph;
742 /* update precision */
743 if (l_product > *p_max_prec) {
744 *p_max_prec = l_product;
753 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
754 const opj_cp_t *p_cp,
760 OPJ_UINT32 * p_dx_min,
761 OPJ_UINT32 * p_dy_min,
762 OPJ_UINT32 * p_max_prec,
763 OPJ_UINT32 * p_max_res,
764 OPJ_UINT32 ** p_resolutions)
767 OPJ_UINT32 compno, resno;
770 const opj_tcp_t *tcp = 00;
771 const opj_tccp_t * l_tccp = 00;
772 const opj_image_comp_t * l_img_comp = 00;
774 /* to store l_dx, l_dy, w and h for each resolution and component.*/
775 OPJ_UINT32 * lResolutionPtr;
777 /* position in x and y of tile*/
780 /* non-corrected (in regard to image offset) tile offset */
781 OPJ_UINT32 l_tx0, l_ty0;
783 /* preconditions in debug*/
785 assert(p_image != 00);
786 assert(tileno < p_cp->tw * p_cp->th);
789 tcp = &p_cp->tcps [tileno];
791 l_img_comp = p_image->comps;
793 /* position in x and y of tile*/
794 p = tileno % p_cp->tw;
795 q = tileno / p_cp->tw;
797 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
798 l_tx0 = p_cp->tx0 + p *
799 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
800 *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
801 *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
802 l_ty0 = p_cp->ty0 + q *
803 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
804 *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
805 *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
807 /* max precision and resolution is 0 (can only grow)*/
811 /* take the largest value for dx_min and dy_min*/
812 *p_dx_min = 0x7fffffff;
813 *p_dy_min = 0x7fffffff;
815 for (compno = 0; compno < p_image->numcomps; ++compno) {
816 /* aritmetic variables to calculate*/
817 OPJ_UINT32 l_level_no;
818 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
819 OPJ_INT32 l_px0, l_py0, l_px1, py1;
820 OPJ_UINT32 l_product;
821 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
822 OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
824 lResolutionPtr = p_resolutions[compno];
826 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
827 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
828 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
829 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
831 if (l_tccp->numresolutions > *p_max_res) {
832 *p_max_res = l_tccp->numresolutions;
835 /* use custom size for precincts*/
836 l_level_no = l_tccp->numresolutions;
837 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
838 OPJ_UINT32 l_dx, l_dy;
842 /* precinct width and height*/
843 l_pdx = l_tccp->prcw[resno];
844 l_pdy = l_tccp->prch[resno];
845 *lResolutionPtr++ = l_pdx;
846 *lResolutionPtr++ = l_pdy;
847 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
848 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
849 /* take the minimum size for l_dx for each comp and resolution*/
850 *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dx_min, (OPJ_INT32)l_dx);
851 *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dy_min, (OPJ_INT32)l_dy);
853 /* various calculations of extents*/
854 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
855 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
856 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
857 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
858 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
859 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
860 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
861 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
862 l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
863 l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
864 *lResolutionPtr++ = l_pw;
865 *lResolutionPtr++ = l_ph;
866 l_product = l_pw * l_ph;
868 /* update precision*/
869 if (l_product > *p_max_prec) {
870 *p_max_prec = l_product;
879 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
884 OPJ_UINT32 pino, compno;
885 /* number of poc in the p_pi*/
886 OPJ_UINT32 l_poc_bound;
888 /* pointers to tile coding parameters and components.*/
889 opj_pi_iterator_t *l_pi = 00;
891 const opj_tccp_t *tccp = 00;
893 /* current packet iterator being allocated*/
894 opj_pi_iterator_t *l_current_pi = 00;
896 /* preconditions in debug*/
899 assert(tileno < cp->tw * cp->th);
902 tcp = &cp->tcps[tileno];
903 l_poc_bound = tcp->numpocs + 1;
905 /* memory allocations*/
906 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
907 sizeof(opj_pi_iterator_t));
913 for (pino = 0; pino < l_poc_bound ; ++pino) {
915 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
916 sizeof(opj_pi_comp_t));
917 if (! l_current_pi->comps) {
918 opj_pi_destroy(l_pi, l_poc_bound);
922 l_current_pi->numcomps = image->numcomps;
924 for (compno = 0; compno < image->numcomps; ++compno) {
925 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
927 tccp = &tcp->tccps[compno];
929 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
930 sizeof(opj_pi_resolution_t));
931 if (!comp->resolutions) {
932 opj_pi_destroy(l_pi, l_poc_bound);
936 comp->numresolutions = tccp->numresolutions;
943 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
949 OPJ_UINT32 p_max_prec,
950 OPJ_UINT32 p_max_res,
956 /* tile coding parameter*/
957 opj_tcp_t *l_tcp = 00;
958 /* current poc being updated*/
959 opj_poc_t * l_current_poc = 00;
962 OPJ_UINT32 l_poc_bound;
964 OPJ_ARG_NOT_USED(p_max_res);
966 /* preconditions in debug*/
968 assert(p_tileno < p_cp->tw * p_cp->th);
971 l_tcp = &p_cp->tcps [p_tileno];
972 /* number of iterations in the loop */
973 l_poc_bound = l_tcp->numpocs + 1;
975 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
976 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
977 l_current_poc = l_tcp->pocs;
979 l_current_poc->compS = l_current_poc->compno0;
980 l_current_poc->compE = l_current_poc->compno1;
981 l_current_poc->resS = l_current_poc->resno0;
982 l_current_poc->resE = l_current_poc->resno1;
983 l_current_poc->layE = l_current_poc->layno1;
985 /* special treatment for the first element*/
986 l_current_poc->layS = 0;
987 l_current_poc->prg = l_current_poc->prg1;
988 l_current_poc->prcS = 0;
990 l_current_poc->prcE = p_max_prec;
991 l_current_poc->txS = (OPJ_UINT32)p_tx0;
992 l_current_poc->txE = (OPJ_UINT32)p_tx1;
993 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
994 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
995 l_current_poc->dx = p_dx_min;
996 l_current_poc->dy = p_dy_min;
999 for (pino = 1; pino < l_poc_bound ; ++pino) {
1000 l_current_poc->compS = l_current_poc->compno0;
1001 l_current_poc->compE = l_current_poc->compno1;
1002 l_current_poc->resS = l_current_poc->resno0;
1003 l_current_poc->resE = l_current_poc->resno1;
1004 l_current_poc->layE = l_current_poc->layno1;
1005 l_current_poc->prg = l_current_poc->prg1;
1006 l_current_poc->prcS = 0;
1007 /* special treatment here different from the first element*/
1008 l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
1009 l_current_poc->layE : 0;
1011 l_current_poc->prcE = p_max_prec;
1012 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1013 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1014 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1015 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1016 l_current_poc->dx = p_dx_min;
1017 l_current_poc->dy = p_dy_min;
1022 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
1023 OPJ_UINT32 p_num_comps,
1024 OPJ_UINT32 p_tileno,
1029 OPJ_UINT32 p_max_prec,
1030 OPJ_UINT32 p_max_res,
1031 OPJ_UINT32 p_dx_min,
1032 OPJ_UINT32 p_dy_min)
1036 /* tile coding parameter*/
1037 opj_tcp_t *l_tcp = 00;
1038 /* current poc being updated*/
1039 opj_poc_t * l_current_poc = 00;
1041 OPJ_UINT32 l_poc_bound;
1043 /* preconditions in debug*/
1045 assert(p_tileno < p_cp->tw * p_cp->th);
1047 /* initializations*/
1048 l_tcp = &p_cp->tcps [p_tileno];
1050 /* number of iterations in the loop */
1051 l_poc_bound = l_tcp->numpocs + 1;
1053 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1054 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1055 l_current_poc = l_tcp->pocs;
1057 for (pino = 0; pino < l_poc_bound ; ++pino) {
1058 l_current_poc->compS = 0;
1059 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1060 l_current_poc->resS = 0;
1061 l_current_poc->resE = p_max_res;
1062 l_current_poc->layS = 0;
1063 l_current_poc->layE = l_tcp->numlayers;
1064 l_current_poc->prg = l_tcp->prg;
1065 l_current_poc->prcS = 0;
1066 l_current_poc->prcE = p_max_prec;
1067 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1068 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1069 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1070 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1071 l_current_poc->dx = p_dx_min;
1072 l_current_poc->dy = p_dy_min;
1077 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1079 OPJ_UINT32 p_max_precision,
1080 OPJ_UINT32 p_max_res)
1085 /* encoding prameters to set*/
1088 opj_pi_iterator_t * l_current_pi = 00;
1089 opj_poc_t* l_current_poc = 0;
1091 OPJ_ARG_NOT_USED(p_max_res);
1093 /* preconditions in debug*/
1095 assert(p_tcp != 00);
1097 /* initializations*/
1098 l_bound = p_tcp->numpocs + 1;
1099 l_current_pi = p_pi;
1100 l_current_poc = p_tcp->pocs;
1102 for (pino = 0; pino < l_bound; ++pino) {
1103 l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1104 l_current_pi->first = 1;
1106 l_current_pi->poc.resno0 =
1107 l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1108 l_current_pi->poc.compno0 =
1109 l_current_poc->compno0; /* Component Index #0 (Start) */
1110 l_current_pi->poc.layno0 = 0;
1111 l_current_pi->poc.precno0 = 0;
1112 l_current_pi->poc.resno1 =
1113 l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1114 l_current_pi->poc.compno1 =
1115 l_current_poc->compno1; /* Component Index #0 (End) */
1116 l_current_pi->poc.layno1 = l_current_poc->layno1; /* Layer Index #0 (End) */
1117 l_current_pi->poc.precno1 = p_max_precision;
1123 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1125 OPJ_UINT32 p_max_precision,
1126 OPJ_UINT32 p_max_res)
1131 /* encoding prameters to set*/
1134 opj_pi_iterator_t * l_current_pi = 00;
1135 /* preconditions in debug*/
1136 assert(p_tcp != 00);
1139 /* initializations*/
1140 l_bound = p_tcp->numpocs + 1;
1141 l_current_pi = p_pi;
1143 for (pino = 0; pino < l_bound; ++pino) {
1144 l_current_pi->poc.prg = p_tcp->prg;
1145 l_current_pi->first = 1;
1146 l_current_pi->poc.resno0 = 0;
1147 l_current_pi->poc.compno0 = 0;
1148 l_current_pi->poc.layno0 = 0;
1149 l_current_pi->poc.precno0 = 0;
1150 l_current_pi->poc.resno1 = p_max_res;
1151 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1152 l_current_pi->poc.layno1 = p_tcp->numlayers;
1153 l_current_pi->poc.precno1 = p_max_precision;
1160 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1164 const OPJ_CHAR *prog)
1167 opj_tcp_t *tcps = &cp->tcps[tileno];
1168 opj_poc_t *tcp = &tcps->pocs[pino];
1171 for (i = pos; pos >= 0; i--) {
1174 if (tcp->res_t == tcp->resE) {
1175 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1185 if (tcp->comp_t == tcp->compE) {
1186 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1196 if (tcp->lay_t == tcp->layE) {
1197 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1208 case OPJ_LRCP: /* fall through */
1210 if (tcp->prc_t == tcp->prcE) {
1211 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1221 if (tcp->tx0_t == tcp->txE) {
1223 if (tcp->ty0_t == tcp->tyE) {
1224 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1245 ==========================================================
1246 Packet iterator interface
1247 ==========================================================
1249 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1251 OPJ_UINT32 p_tile_no)
1255 OPJ_UINT32 compno, resno;
1257 /* to store w, h, dx and dy fro all components and resolutions */
1258 OPJ_UINT32 * l_tmp_data;
1259 OPJ_UINT32 ** l_tmp_ptr;
1261 /* encoding prameters to set */
1262 OPJ_UINT32 l_max_res;
1263 OPJ_UINT32 l_max_prec;
1264 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1265 OPJ_UINT32 l_dx_min, l_dy_min;
1267 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1268 OPJ_UINT32 l_data_stride;
1271 opj_pi_iterator_t *l_pi = 00;
1272 opj_tcp_t *l_tcp = 00;
1273 const opj_tccp_t *l_tccp = 00;
1274 opj_pi_comp_t *l_current_comp = 00;
1275 opj_image_comp_t * l_img_comp = 00;
1276 opj_pi_iterator_t * l_current_pi = 00;
1277 OPJ_UINT32 * l_encoding_value_ptr = 00;
1279 /* preconditions in debug */
1281 assert(p_image != 00);
1282 assert(p_tile_no < p_cp->tw * p_cp->th);
1284 /* initializations */
1285 l_tcp = &p_cp->tcps[p_tile_no];
1286 l_bound = l_tcp->numpocs + 1;
1288 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1289 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1290 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1295 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1296 p_image->numcomps * sizeof(OPJ_UINT32 *));
1299 opj_free(l_tmp_data);
1303 /* memory allocation for pi */
1304 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1306 opj_free(l_tmp_data);
1307 opj_free(l_tmp_ptr);
1311 l_encoding_value_ptr = l_tmp_data;
1312 /* update pointer array */
1314 (compno = 0; compno < p_image->numcomps; ++compno) {
1315 l_tmp_ptr[compno] = l_encoding_value_ptr;
1316 l_encoding_value_ptr += l_data_stride;
1318 /* get encoding parameters */
1319 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1320 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1322 /* step calculations */
1324 l_step_c = l_max_prec * l_step_p;
1325 l_step_r = p_image->numcomps * l_step_c;
1326 l_step_l = l_max_res * l_step_r;
1328 /* set values for first packet iterator */
1329 l_current_pi = l_pi;
1331 /* memory allocation for include */
1332 /* prevent an integer overflow issue */
1333 /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1334 l_current_pi->include = 00;
1335 if (l_step_l <= (SIZE_MAX / (l_tcp->numlayers + 1U))) {
1336 l_current_pi->include = (OPJ_INT16*) opj_calloc((size_t)(
1337 l_tcp->numlayers + 1U) * l_step_l, sizeof(OPJ_INT16));
1340 if (!l_current_pi->include) {
1341 opj_free(l_tmp_data);
1342 opj_free(l_tmp_ptr);
1343 opj_pi_destroy(l_pi, l_bound);
1347 /* special treatment for the first packet iterator */
1348 l_current_comp = l_current_pi->comps;
1349 l_img_comp = p_image->comps;
1350 l_tccp = l_tcp->tccps;
1352 l_current_pi->tx0 = l_tx0;
1353 l_current_pi->ty0 = l_ty0;
1354 l_current_pi->tx1 = l_tx1;
1355 l_current_pi->ty1 = l_ty1;
1357 /*l_current_pi->dx = l_img_comp->dx;*/
1358 /*l_current_pi->dy = l_img_comp->dy;*/
1360 l_current_pi->step_p = l_step_p;
1361 l_current_pi->step_c = l_step_c;
1362 l_current_pi->step_r = l_step_r;
1363 l_current_pi->step_l = l_step_l;
1365 /* allocation for components and number of components has already been calculated by opj_pi_create */
1367 (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1368 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1369 l_encoding_value_ptr = l_tmp_ptr[compno];
1371 l_current_comp->dx = l_img_comp->dx;
1372 l_current_comp->dy = l_img_comp->dy;
1373 /* resolutions have already been initialized */
1375 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1376 l_res->pdx = *(l_encoding_value_ptr++);
1377 l_res->pdy = *(l_encoding_value_ptr++);
1378 l_res->pw = *(l_encoding_value_ptr++);
1379 l_res->ph = *(l_encoding_value_ptr++);
1388 for (pino = 1 ; pino < l_bound ; ++pino) {
1389 l_current_comp = l_current_pi->comps;
1390 l_img_comp = p_image->comps;
1391 l_tccp = l_tcp->tccps;
1393 l_current_pi->tx0 = l_tx0;
1394 l_current_pi->ty0 = l_ty0;
1395 l_current_pi->tx1 = l_tx1;
1396 l_current_pi->ty1 = l_ty1;
1397 /*l_current_pi->dx = l_dx_min;*/
1398 /*l_current_pi->dy = l_dy_min;*/
1399 l_current_pi->step_p = l_step_p;
1400 l_current_pi->step_c = l_step_c;
1401 l_current_pi->step_r = l_step_r;
1402 l_current_pi->step_l = l_step_l;
1404 /* allocation for components and number of components has already been calculated by opj_pi_create */
1406 (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1407 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1408 l_encoding_value_ptr = l_tmp_ptr[compno];
1410 l_current_comp->dx = l_img_comp->dx;
1411 l_current_comp->dy = l_img_comp->dy;
1412 /* resolutions have already been initialized */
1414 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1415 l_res->pdx = *(l_encoding_value_ptr++);
1416 l_res->pdy = *(l_encoding_value_ptr++);
1417 l_res->pw = *(l_encoding_value_ptr++);
1418 l_res->ph = *(l_encoding_value_ptr++);
1425 /* special treatment*/
1426 l_current_pi->include = (l_current_pi - 1)->include;
1429 opj_free(l_tmp_data);
1431 opj_free(l_tmp_ptr);
1435 opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1437 opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1444 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1446 OPJ_UINT32 p_tile_no,
1447 J2K_T2_MODE p_t2_mode)
1451 OPJ_UINT32 compno, resno;
1453 /* to store w, h, dx and dy fro all components and resolutions*/
1454 OPJ_UINT32 * l_tmp_data;
1455 OPJ_UINT32 ** l_tmp_ptr;
1457 /* encoding prameters to set*/
1458 OPJ_UINT32 l_max_res;
1459 OPJ_UINT32 l_max_prec;
1460 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1461 OPJ_UINT32 l_dx_min, l_dy_min;
1463 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1464 OPJ_UINT32 l_data_stride;
1467 opj_pi_iterator_t *l_pi = 00;
1468 opj_tcp_t *l_tcp = 00;
1469 const opj_tccp_t *l_tccp = 00;
1470 opj_pi_comp_t *l_current_comp = 00;
1471 opj_image_comp_t * l_img_comp = 00;
1472 opj_pi_iterator_t * l_current_pi = 00;
1473 OPJ_UINT32 * l_encoding_value_ptr = 00;
1475 /* preconditions in debug*/
1477 assert(p_image != 00);
1478 assert(p_tile_no < p_cp->tw * p_cp->th);
1480 /* initializations*/
1481 l_tcp = &p_cp->tcps[p_tile_no];
1482 l_bound = l_tcp->numpocs + 1;
1484 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1485 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1486 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1491 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1492 p_image->numcomps * sizeof(OPJ_UINT32 *));
1494 opj_free(l_tmp_data);
1498 /* memory allocation for pi*/
1499 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1501 opj_free(l_tmp_data);
1502 opj_free(l_tmp_ptr);
1506 l_encoding_value_ptr = l_tmp_data;
1507 /* update pointer array*/
1508 for (compno = 0; compno < p_image->numcomps; ++compno) {
1509 l_tmp_ptr[compno] = l_encoding_value_ptr;
1510 l_encoding_value_ptr += l_data_stride;
1513 /* get encoding parameters*/
1514 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1515 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1517 /* step calculations*/
1519 l_step_c = l_max_prec * l_step_p;
1520 l_step_r = p_image->numcomps * l_step_c;
1521 l_step_l = l_max_res * l_step_r;
1523 /* set values for first packet iterator*/
1524 l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1525 l_current_pi = l_pi;
1527 /* memory allocation for include*/
1528 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l,
1530 if (!l_current_pi->include) {
1531 opj_free(l_tmp_data);
1532 opj_free(l_tmp_ptr);
1533 opj_pi_destroy(l_pi, l_bound);
1537 /* special treatment for the first packet iterator*/
1538 l_current_comp = l_current_pi->comps;
1539 l_img_comp = p_image->comps;
1540 l_tccp = l_tcp->tccps;
1541 l_current_pi->tx0 = l_tx0;
1542 l_current_pi->ty0 = l_ty0;
1543 l_current_pi->tx1 = l_tx1;
1544 l_current_pi->ty1 = l_ty1;
1545 l_current_pi->dx = l_dx_min;
1546 l_current_pi->dy = l_dy_min;
1547 l_current_pi->step_p = l_step_p;
1548 l_current_pi->step_c = l_step_c;
1549 l_current_pi->step_r = l_step_r;
1550 l_current_pi->step_l = l_step_l;
1552 /* allocation for components and number of components has already been calculated by opj_pi_create */
1553 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1554 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1555 l_encoding_value_ptr = l_tmp_ptr[compno];
1557 l_current_comp->dx = l_img_comp->dx;
1558 l_current_comp->dy = l_img_comp->dy;
1560 /* resolutions have already been initialized */
1561 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1562 l_res->pdx = *(l_encoding_value_ptr++);
1563 l_res->pdy = *(l_encoding_value_ptr++);
1564 l_res->pw = *(l_encoding_value_ptr++);
1565 l_res->ph = *(l_encoding_value_ptr++);
1575 for (pino = 1 ; pino < l_bound ; ++pino) {
1576 l_current_comp = l_current_pi->comps;
1577 l_img_comp = p_image->comps;
1578 l_tccp = l_tcp->tccps;
1580 l_current_pi->tx0 = l_tx0;
1581 l_current_pi->ty0 = l_ty0;
1582 l_current_pi->tx1 = l_tx1;
1583 l_current_pi->ty1 = l_ty1;
1584 l_current_pi->dx = l_dx_min;
1585 l_current_pi->dy = l_dy_min;
1586 l_current_pi->step_p = l_step_p;
1587 l_current_pi->step_c = l_step_c;
1588 l_current_pi->step_r = l_step_r;
1589 l_current_pi->step_l = l_step_l;
1591 /* allocation for components and number of components has already been calculated by opj_pi_create */
1592 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1593 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1594 l_encoding_value_ptr = l_tmp_ptr[compno];
1596 l_current_comp->dx = l_img_comp->dx;
1597 l_current_comp->dy = l_img_comp->dy;
1598 /* resolutions have already been initialized */
1599 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1600 l_res->pdx = *(l_encoding_value_ptr++);
1601 l_res->pdy = *(l_encoding_value_ptr++);
1602 l_res->pw = *(l_encoding_value_ptr++);
1603 l_res->ph = *(l_encoding_value_ptr++);
1611 /* special treatment*/
1612 l_current_pi->include = (l_current_pi - 1)->include;
1616 opj_free(l_tmp_data);
1618 opj_free(l_tmp_ptr);
1621 if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1622 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1623 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1625 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
1626 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1632 void opj_pi_create_encode(opj_pi_iterator_t *pi,
1638 J2K_T2_MODE t2_mode)
1640 const OPJ_CHAR *prog;
1642 OPJ_UINT32 incr_top = 1, resetX = 0;
1643 opj_tcp_t *tcps = &cp->tcps[tileno];
1644 opj_poc_t *tcp = &tcps->pocs[pino];
1646 prog = opj_j2k_convert_progression_order(tcp->prg);
1649 pi[pino].poc.prg = tcp->prg;
1651 if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1652 (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz)))) {
1653 pi[pino].poc.resno0 = tcp->resS;
1654 pi[pino].poc.resno1 = tcp->resE;
1655 pi[pino].poc.compno0 = tcp->compS;
1656 pi[pino].poc.compno1 = tcp->compE;
1657 pi[pino].poc.layno0 = tcp->layS;
1658 pi[pino].poc.layno1 = tcp->layE;
1659 pi[pino].poc.precno0 = tcp->prcS;
1660 pi[pino].poc.precno1 = tcp->prcE;
1661 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1662 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1663 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1664 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1666 for (i = tppos + 1; i < 4; i++) {
1669 pi[pino].poc.resno0 = tcp->resS;
1670 pi[pino].poc.resno1 = tcp->resE;
1673 pi[pino].poc.compno0 = tcp->compS;
1674 pi[pino].poc.compno1 = tcp->compE;
1677 pi[pino].poc.layno0 = tcp->layS;
1678 pi[pino].poc.layno1 = tcp->layE;
1684 pi[pino].poc.precno0 = tcp->prcS;
1685 pi[pino].poc.precno1 = tcp->prcE;
1688 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1689 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1690 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1691 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1699 for (i = tppos; i >= 0; i--) {
1702 tcp->comp_t = tcp->compS;
1703 pi[pino].poc.compno0 = tcp->comp_t;
1704 pi[pino].poc.compno1 = tcp->comp_t + 1;
1708 tcp->res_t = tcp->resS;
1709 pi[pino].poc.resno0 = tcp->res_t;
1710 pi[pino].poc.resno1 = tcp->res_t + 1;
1714 tcp->lay_t = tcp->layS;
1715 pi[pino].poc.layno0 = tcp->lay_t;
1716 pi[pino].poc.layno1 = tcp->lay_t + 1;
1723 tcp->prc_t = tcp->prcS;
1724 pi[pino].poc.precno0 = tcp->prc_t;
1725 pi[pino].poc.precno1 = tcp->prc_t + 1;
1729 tcp->tx0_t = tcp->txS;
1730 tcp->ty0_t = tcp->tyS;
1731 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1732 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1733 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1734 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1735 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1736 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1744 for (i = tppos; i >= 0; i--) {
1747 pi[pino].poc.compno0 = tcp->comp_t - 1;
1748 pi[pino].poc.compno1 = tcp->comp_t;
1751 pi[pino].poc.resno0 = tcp->res_t - 1;
1752 pi[pino].poc.resno1 = tcp->res_t;
1755 pi[pino].poc.layno0 = tcp->lay_t - 1;
1756 pi[pino].poc.layno1 = tcp->lay_t;
1762 pi[pino].poc.precno0 = tcp->prc_t - 1;
1763 pi[pino].poc.precno1 = tcp->prc_t;
1766 pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
1767 pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
1768 pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
1769 pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
1774 if (incr_top == 1) {
1777 if (tcp->res_t == tcp->resE) {
1778 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1779 tcp->res_t = tcp->resS;
1780 pi[pino].poc.resno0 = tcp->res_t;
1781 pi[pino].poc.resno1 = tcp->res_t + 1;
1788 pi[pino].poc.resno0 = tcp->res_t;
1789 pi[pino].poc.resno1 = tcp->res_t + 1;
1795 if (tcp->comp_t == tcp->compE) {
1796 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1797 tcp->comp_t = tcp->compS;
1798 pi[pino].poc.compno0 = tcp->comp_t;
1799 pi[pino].poc.compno1 = tcp->comp_t + 1;
1806 pi[pino].poc.compno0 = tcp->comp_t;
1807 pi[pino].poc.compno1 = tcp->comp_t + 1;
1813 if (tcp->lay_t == tcp->layE) {
1814 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1815 tcp->lay_t = tcp->layS;
1816 pi[pino].poc.layno0 = tcp->lay_t;
1817 pi[pino].poc.layno1 = tcp->lay_t + 1;
1824 pi[pino].poc.layno0 = tcp->lay_t;
1825 pi[pino].poc.layno1 = tcp->lay_t + 1;
1834 if (tcp->prc_t == tcp->prcE) {
1835 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1836 tcp->prc_t = tcp->prcS;
1837 pi[pino].poc.precno0 = tcp->prc_t;
1838 pi[pino].poc.precno1 = tcp->prc_t + 1;
1845 pi[pino].poc.precno0 = tcp->prc_t;
1846 pi[pino].poc.precno1 = tcp->prc_t + 1;
1852 if (tcp->tx0_t >= tcp->txE) {
1853 if (tcp->ty0_t >= tcp->tyE) {
1854 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1855 tcp->ty0_t = tcp->tyS;
1856 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1857 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1858 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1866 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1867 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1868 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1873 tcp->tx0_t = tcp->txS;
1874 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1875 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1876 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1879 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1880 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1881 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1894 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
1895 OPJ_UINT32 p_nb_elements)
1897 OPJ_UINT32 compno, pino;
1898 opj_pi_iterator_t *l_current_pi = p_pi;
1900 if (p_pi->include) {
1901 opj_free(p_pi->include);
1904 for (pino = 0; pino < p_nb_elements; ++pino) {
1905 if (l_current_pi->comps) {
1906 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1907 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
1908 if (l_current_component->resolutions) {
1909 opj_free(l_current_component->resolutions);
1910 l_current_component->resolutions = 00;
1913 ++l_current_component;
1915 opj_free(l_current_pi->comps);
1916 l_current_pi->comps = 0;
1926 void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
1928 OPJ_UINT32 p_tile_no)
1930 /* encoding parameters to set */
1931 OPJ_UINT32 l_max_res;
1932 OPJ_UINT32 l_max_prec;
1933 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1934 OPJ_UINT32 l_dx_min, l_dy_min;
1937 opj_tcp_t *l_tcp = 00;
1941 assert(p_image != 00);
1942 assert(p_tile_no < p_cp->tw * p_cp->th);
1944 l_tcp = &(p_cp->tcps[p_tile_no]);
1946 /* get encoding parameters */
1947 opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
1948 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
1951 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1952 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1954 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
1955 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1959 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
1961 switch (pi->poc.prg) {
1963 return opj_pi_next_lrcp(pi);
1965 return opj_pi_next_rlcp(pi);
1967 return opj_pi_next_rpcl(pi);
1969 return opj_pi_next_pcrl(pi);
1971 return opj_pi_next_cprl(pi);
1972 case OPJ_PROG_UNKNOWN: