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 #define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_UINT32
41 #include "opj_includes.h"
43 /** @defgroup PI PI - Implementation of a packet iterator */
46 /** @name Local static functions */
50 Get next packet in layer-resolution-component-precinct order.
51 @param pi packet iterator to modify
52 @return returns false if pi pointed to the last packet or else returns true
54 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
56 Get next packet in resolution-layer-component-precinct order.
57 @param pi packet iterator to modify
58 @return returns false if pi pointed to the last packet or else returns true
60 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
62 Get next packet in resolution-precinct-component-layer order.
63 @param pi packet iterator to modify
64 @return returns false if pi pointed to the last packet or else returns true
66 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
68 Get next packet in precinct-component-resolution-layer order.
69 @param pi packet iterator to modify
70 @return returns false if pi pointed to the last packet or else returns true
72 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
74 Get next packet in component-precinct-resolution-layer order.
75 @param pi packet iterator to modify
76 @return returns false if pi pointed to the last packet or else returns true
78 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
81 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
83 * @param p_cp the coding parameters to modify
84 * @param p_tileno the tile index being concerned.
85 * @param p_tx0 X0 parameter for the tile
86 * @param p_tx1 X1 parameter for the tile
87 * @param p_ty0 Y0 parameter for the tile
88 * @param p_ty1 Y1 parameter for the tile
89 * @param p_max_prec the maximum precision for all the bands of the tile
90 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
91 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
92 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
94 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
100 OPJ_UINT32 p_max_prec,
101 OPJ_UINT32 p_max_res,
103 OPJ_UINT32 p_dy_min);
106 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
108 * @param p_cp the coding parameters to modify
109 * @param p_num_comps the number of components
110 * @param p_tileno the tile index being concerned.
111 * @param p_tx0 X0 parameter for the tile
112 * @param p_tx1 X1 parameter for the tile
113 * @param p_ty0 Y0 parameter for the tile
114 * @param p_ty1 Y1 parameter for the tile
115 * @param p_max_prec the maximum precision for all the bands of the tile
116 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
117 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
118 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
120 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
121 OPJ_UINT32 p_num_comps,
127 OPJ_UINT32 p_max_prec,
128 OPJ_UINT32 p_max_res,
130 OPJ_UINT32 p_dy_min);
132 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
134 * @param p_image the image being encoded.
135 * @param p_cp the coding parameters.
136 * @param tileno the tile index of the tile being encoded.
137 * @param p_tx0 pointer that will hold the X0 parameter for the tile
138 * @param p_tx1 pointer that will hold the X1 parameter for the tile
139 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
140 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
141 * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
142 * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
143 * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
144 * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
146 static void opj_get_encoding_parameters(const opj_image_t *p_image,
147 const opj_cp_t *p_cp,
153 OPJ_UINT32 * p_dx_min,
154 OPJ_UINT32 * p_dy_min,
155 OPJ_UINT32 * p_max_prec,
156 OPJ_UINT32 * p_max_res);
159 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
160 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
161 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
162 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
163 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
165 * @param p_image the image being encoded.
166 * @param p_cp the coding parameters.
167 * @param tileno the tile index of the tile being encoded.
168 * @param p_tx0 pointer that will hold the X0 parameter for the tile
169 * @param p_tx1 pointer that will hold the X1 parameter for the tile
170 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
171 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
172 * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
173 * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
174 * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
175 * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
176 * @param p_resolutions pointer to an area corresponding to the one described above.
178 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
179 const opj_cp_t *p_cp,
185 OPJ_UINT32 * p_dx_min,
186 OPJ_UINT32 * p_dy_min,
187 OPJ_UINT32 * p_max_prec,
188 OPJ_UINT32 * p_max_res,
189 OPJ_UINT32 ** p_resolutions);
191 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
192 * No other data is set. The include section of the packet iterator is not allocated.
194 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
195 * @param p_cp the coding parameters.
196 * @param tileno the index of the tile from which creating the packet iterator.
197 * @param manager Event manager
199 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
200 const opj_cp_t *p_cp,
202 opj_event_mgr_t* manager);
206 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
208 OPJ_UINT32 p_max_precision,
209 OPJ_UINT32 p_max_res);
213 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
215 OPJ_UINT32 p_max_precision,
216 OPJ_UINT32 p_max_res);
221 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
225 const OPJ_CHAR *prog);
232 ==========================================================
234 ==========================================================
237 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
239 opj_pi_comp_t *comp = NULL;
240 opj_pi_resolution_t *res = NULL;
241 OPJ_UINT32 index = 0;
243 if (pi->poc.compno0 >= pi->numcomps ||
244 pi->poc.compno1 >= pi->numcomps + 1) {
245 opj_event_msg(pi->manager, EVT_ERROR,
246 "opj_pi_next_lrcp(): invalid compno0/compno1\n");
251 comp = &pi->comps[pi->compno];
252 res = &comp->resolutions[pi->resno];
258 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
259 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
261 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
262 comp = &pi->comps[pi->compno];
263 if (pi->resno >= comp->numresolutions) {
266 res = &comp->resolutions[pi->resno];
268 pi->poc.precno1 = res->pw * res->ph;
270 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
271 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
272 pi->step_c + pi->precno * pi->step_p;
273 /* Avoids index out of bounds access with */
274 /* id_000098,sig_11,src_005411,op_havoc,rep_2 of */
275 /* https://github.com/uclouvain/openjpeg/issues/938 */
276 /* Not sure if this is the most clever fix. Perhaps */
277 /* include should be resized when a POC arises, or */
278 /* the POC should be rejected */
279 if (index >= pi->include_size) {
280 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
283 if (!pi->include[index]) {
284 pi->include[index] = 1;
297 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
299 opj_pi_comp_t *comp = NULL;
300 opj_pi_resolution_t *res = NULL;
301 OPJ_UINT32 index = 0;
303 if (pi->poc.compno0 >= pi->numcomps ||
304 pi->poc.compno1 >= pi->numcomps + 1) {
305 opj_event_msg(pi->manager, EVT_ERROR,
306 "opj_pi_next_rlcp(): invalid compno0/compno1\n");
311 comp = &pi->comps[pi->compno];
312 res = &comp->resolutions[pi->resno];
318 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
319 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
320 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
321 comp = &pi->comps[pi->compno];
322 if (pi->resno >= comp->numresolutions) {
325 res = &comp->resolutions[pi->resno];
327 pi->poc.precno1 = res->pw * res->ph;
329 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
330 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
331 pi->step_c + pi->precno * pi->step_p;
332 if (index >= pi->include_size) {
333 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
336 if (!pi->include[index]) {
337 pi->include[index] = 1;
350 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
352 opj_pi_comp_t *comp = NULL;
353 opj_pi_resolution_t *res = NULL;
354 OPJ_UINT32 index = 0;
356 if (pi->poc.compno0 >= pi->numcomps ||
357 pi->poc.compno1 >= pi->numcomps + 1) {
358 opj_event_msg(pi->manager, EVT_ERROR,
359 "opj_pi_next_rpcl(): invalid compno0/compno1\n");
366 OPJ_UINT32 compno, resno;
370 for (compno = 0; compno < pi->numcomps; compno++) {
371 comp = &pi->comps[compno];
372 for (resno = 0; resno < comp->numresolutions; resno++) {
374 res = &comp->resolutions[resno];
375 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
376 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
377 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
378 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
380 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
381 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
382 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
383 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
387 if (pi->dx == 0 || pi->dy == 0) {
392 pi->poc.ty0 = pi->ty0;
393 pi->poc.tx0 = pi->tx0;
394 pi->poc.ty1 = pi->ty1;
395 pi->poc.tx1 = pi->tx1;
397 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
398 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
399 pi->y += (pi->dy - (pi->y % pi->dy))) {
400 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
401 pi->x += (pi->dx - (pi->x % pi->dx))) {
402 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
404 OPJ_UINT32 trx0, try0;
405 OPJ_UINT32 trx1, try1;
407 OPJ_UINT32 prci, prcj;
408 comp = &pi->comps[pi->compno];
409 if (pi->resno >= comp->numresolutions) {
412 res = &comp->resolutions[pi->resno];
413 levelno = comp->numresolutions - 1 - pi->resno;
414 /* Avoids division by zero */
415 /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
416 /* of https://github.com/uclouvain/openjpeg/issues/938 */
418 ((comp->dx << levelno) >> levelno) != comp->dx ||
419 ((comp->dy << levelno) >> levelno) != comp->dy) {
422 if ((comp->dx << levelno) > INT_MAX ||
423 (comp->dy << levelno) > INT_MAX) {
426 trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
427 try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
428 trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
429 try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
430 rpx = res->pdx + levelno;
431 rpy = res->pdy + levelno;
433 /* To avoid divisions by zero / undefined behaviour on shift */
435 /* Fixes reading id:000026,sig:08,src:002419,op:int32,pos:60,val:+32 */
436 /* of https://github.com/uclouvain/openjpeg/issues/938 */
437 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
438 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
442 /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
443 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
444 ((try0 << levelno) % (1U << rpy))))) {
447 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
448 ((trx0 << levelno) % (1U << rpx))))) {
452 if ((res->pw == 0) || (res->ph == 0)) {
456 if ((trx0 == trx1) || (try0 == try1)) {
460 prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
461 (comp->dx << levelno)), res->pdx)
462 - opj_uint_floordivpow2(trx0, res->pdx);
463 prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
464 (comp->dy << levelno)), res->pdy)
465 - opj_uint_floordivpow2(try0, res->pdy);
466 pi->precno = prci + prcj * res->pw;
467 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
468 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
469 pi->step_c + pi->precno * pi->step_p;
470 if (index >= pi->include_size) {
471 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
474 if (!pi->include[index]) {
475 pi->include[index] = 1;
489 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
491 opj_pi_comp_t *comp = NULL;
492 opj_pi_resolution_t *res = NULL;
493 OPJ_UINT32 index = 0;
495 if (pi->poc.compno0 >= pi->numcomps ||
496 pi->poc.compno1 >= pi->numcomps + 1) {
497 opj_event_msg(pi->manager, EVT_ERROR,
498 "opj_pi_next_pcrl(): invalid compno0/compno1\n");
503 comp = &pi->comps[pi->compno];
506 OPJ_UINT32 compno, resno;
510 for (compno = 0; compno < pi->numcomps; compno++) {
511 comp = &pi->comps[compno];
512 for (resno = 0; resno < comp->numresolutions; resno++) {
514 res = &comp->resolutions[resno];
515 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
516 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
517 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
518 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
520 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
521 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
522 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
523 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
527 if (pi->dx == 0 || pi->dy == 0) {
532 pi->poc.ty0 = pi->ty0;
533 pi->poc.tx0 = pi->tx0;
534 pi->poc.ty1 = pi->ty1;
535 pi->poc.tx1 = pi->tx1;
537 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
538 pi->y += (pi->dy - (pi->y % pi->dy))) {
539 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
540 pi->x += (pi->dx - (pi->x % pi->dx))) {
541 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
542 comp = &pi->comps[pi->compno];
543 for (pi->resno = pi->poc.resno0;
544 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
546 OPJ_UINT32 trx0, try0;
547 OPJ_UINT32 trx1, try1;
549 OPJ_UINT32 prci, prcj;
550 res = &comp->resolutions[pi->resno];
551 levelno = comp->numresolutions - 1 - pi->resno;
552 /* Avoids division by zero */
553 /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
554 /* of https://github.com/uclouvain/openjpeg/issues/938 */
556 ((comp->dx << levelno) >> levelno) != comp->dx ||
557 ((comp->dy << levelno) >> levelno) != comp->dy) {
560 if ((comp->dx << levelno) > INT_MAX ||
561 (comp->dy << levelno) > INT_MAX) {
564 trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
565 try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
566 trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
567 try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
568 rpx = res->pdx + levelno;
569 rpy = res->pdy + levelno;
571 /* To avoid divisions by zero / undefined behaviour on shift */
573 /* Relates to id:000019,sig:08,src:001098,op:flip1,pos:49 */
574 /* of https://github.com/uclouvain/openjpeg/issues/938 */
575 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
576 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
580 /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
581 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
582 ((try0 << levelno) % (1U << rpy))))) {
585 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
586 ((trx0 << levelno) % (1U << rpx))))) {
590 if ((res->pw == 0) || (res->ph == 0)) {
594 if ((trx0 == trx1) || (try0 == try1)) {
598 prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
599 (comp->dx << levelno)), res->pdx)
600 - opj_uint_floordivpow2(trx0, res->pdx);
601 prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
602 (comp->dy << levelno)), res->pdy)
603 - opj_uint_floordivpow2(try0, res->pdy);
604 pi->precno = prci + prcj * res->pw;
605 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
606 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
607 pi->step_c + pi->precno * pi->step_p;
608 if (index >= pi->include_size) {
609 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
612 if (!pi->include[index]) {
613 pi->include[index] = 1;
627 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
629 opj_pi_comp_t *comp = NULL;
630 opj_pi_resolution_t *res = NULL;
631 OPJ_UINT32 index = 0;
633 if (pi->poc.compno0 >= pi->numcomps ||
634 pi->poc.compno1 >= pi->numcomps + 1) {
635 opj_event_msg(pi->manager, EVT_ERROR,
636 "opj_pi_next_cprl(): invalid compno0/compno1\n");
641 comp = &pi->comps[pi->compno];
647 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
649 comp = &pi->comps[pi->compno];
652 for (resno = 0; resno < comp->numresolutions; resno++) {
654 res = &comp->resolutions[resno];
655 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
656 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
657 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
658 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
660 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
661 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
662 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
663 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
666 if (pi->dx == 0 || pi->dy == 0) {
670 pi->poc.ty0 = pi->ty0;
671 pi->poc.tx0 = pi->tx0;
672 pi->poc.ty1 = pi->ty1;
673 pi->poc.tx1 = pi->tx1;
675 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
676 pi->y += (pi->dy - (pi->y % pi->dy))) {
677 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
678 pi->x += (pi->dx - (pi->x % pi->dx))) {
679 for (pi->resno = pi->poc.resno0;
680 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
682 OPJ_UINT32 trx0, try0;
683 OPJ_UINT32 trx1, try1;
685 OPJ_UINT32 prci, prcj;
686 res = &comp->resolutions[pi->resno];
687 levelno = comp->numresolutions - 1 - pi->resno;
688 /* Avoids division by zero on id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
689 /* of https://github.com/uclouvain/openjpeg/issues/938 */
691 ((comp->dx << levelno) >> levelno) != comp->dx ||
692 ((comp->dy << levelno) >> levelno) != comp->dy) {
695 if ((comp->dx << levelno) > INT_MAX ||
696 (comp->dy << levelno) > INT_MAX) {
699 trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
700 try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
701 trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
702 try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
703 rpx = res->pdx + levelno;
704 rpy = res->pdy + levelno;
706 /* To avoid divisions by zero / undefined behaviour on shift */
708 /* Fixes reading id:000019,sig:08,src:001098,op:flip1,pos:49 */
709 /* of https://github.com/uclouvain/openjpeg/issues/938 */
710 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
711 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
715 /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
716 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
717 ((try0 << levelno) % (1U << rpy))))) {
720 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
721 ((trx0 << levelno) % (1U << rpx))))) {
725 if ((res->pw == 0) || (res->ph == 0)) {
729 if ((trx0 == trx1) || (try0 == try1)) {
733 prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
734 (comp->dx << levelno)), res->pdx)
735 - opj_uint_floordivpow2(trx0, res->pdx);
736 prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
737 (comp->dy << levelno)), res->pdy)
738 - opj_uint_floordivpow2(try0, res->pdy);
739 pi->precno = (OPJ_UINT32)(prci + prcj * res->pw);
740 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
741 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
742 pi->step_c + pi->precno * pi->step_p;
743 if (index >= pi->include_size) {
744 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
747 if (!pi->include[index]) {
748 pi->include[index] = 1;
762 static void opj_get_encoding_parameters(const opj_image_t *p_image,
763 const opj_cp_t *p_cp,
769 OPJ_UINT32 * p_dx_min,
770 OPJ_UINT32 * p_dy_min,
771 OPJ_UINT32 * p_max_prec,
772 OPJ_UINT32 * p_max_res)
775 OPJ_UINT32 compno, resno;
777 const opj_tcp_t *l_tcp = 00;
778 const opj_tccp_t * l_tccp = 00;
779 const opj_image_comp_t * l_img_comp = 00;
781 /* position in x and y of tile */
784 /* non-corrected (in regard to image offset) tile offset */
785 OPJ_UINT32 l_tx0, l_ty0;
789 assert(p_image != 00);
790 assert(p_tileno < p_cp->tw * p_cp->th);
792 /* initializations */
793 l_tcp = &p_cp->tcps [p_tileno];
794 l_img_comp = p_image->comps;
795 l_tccp = l_tcp->tccps;
797 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
798 p = p_tileno % p_cp->tw;
799 q = p_tileno / p_cp->tw;
801 /* find extent of tile */
802 l_tx0 = p_cp->tx0 + p *
803 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
804 *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
805 *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
806 l_ty0 = p_cp->ty0 + q *
807 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
808 *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
809 *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
811 /* max precision is 0 (can only grow) */
815 /* take the largest value for dx_min and dy_min */
816 *p_dx_min = 0x7fffffff;
817 *p_dy_min = 0x7fffffff;
819 for (compno = 0; compno < p_image->numcomps; ++compno) {
820 /* arithmetic variables to calculate */
821 OPJ_UINT32 l_level_no;
822 OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
823 OPJ_UINT32 l_px0, l_py0, l_px1, py1;
824 OPJ_UINT32 l_pdx, l_pdy;
825 OPJ_UINT32 l_pw, l_ph;
826 OPJ_UINT32 l_product;
827 OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
829 l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
830 l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
831 l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
832 l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
834 if (l_tccp->numresolutions > *p_max_res) {
835 *p_max_res = l_tccp->numresolutions;
838 /* use custom size for precincts */
839 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
840 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];
846 l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
847 l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
849 /* take the minimum size for dx for each comp and resolution */
850 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
851 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
853 /* various calculations of extents */
854 l_level_no = l_tccp->numresolutions - 1 - resno;
856 l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
857 l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
858 l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
859 l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
861 l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
862 l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
863 l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
865 py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
867 l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
868 l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
870 l_product = l_pw * l_ph;
872 /* update precision */
873 if (l_product > *p_max_prec) {
874 *p_max_prec = l_product;
883 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
884 const opj_cp_t *p_cp,
890 OPJ_UINT32 * p_dx_min,
891 OPJ_UINT32 * p_dy_min,
892 OPJ_UINT32 * p_max_prec,
893 OPJ_UINT32 * p_max_res,
894 OPJ_UINT32 ** p_resolutions)
897 OPJ_UINT32 compno, resno;
900 const opj_tcp_t *tcp = 00;
901 const opj_tccp_t * l_tccp = 00;
902 const opj_image_comp_t * l_img_comp = 00;
904 /* to store l_dx, l_dy, w and h for each resolution and component.*/
905 OPJ_UINT32 * lResolutionPtr;
907 /* position in x and y of tile*/
910 /* non-corrected (in regard to image offset) tile offset */
911 OPJ_UINT32 l_tx0, l_ty0;
913 /* preconditions in debug*/
915 assert(p_image != 00);
916 assert(tileno < p_cp->tw * p_cp->th);
919 tcp = &p_cp->tcps [tileno];
921 l_img_comp = p_image->comps;
923 /* position in x and y of tile*/
924 p = tileno % p_cp->tw;
925 q = tileno / p_cp->tw;
927 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
928 l_tx0 = p_cp->tx0 + p *
929 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
930 *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
931 *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
932 l_ty0 = p_cp->ty0 + q *
933 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
934 *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
935 *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
937 /* max precision and resolution is 0 (can only grow)*/
941 /* take the largest value for dx_min and dy_min*/
942 *p_dx_min = 0x7fffffff;
943 *p_dy_min = 0x7fffffff;
945 for (compno = 0; compno < p_image->numcomps; ++compno) {
946 /* arithmetic variables to calculate*/
947 OPJ_UINT32 l_level_no;
948 OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
949 OPJ_UINT32 l_px0, l_py0, l_px1, py1;
950 OPJ_UINT32 l_product;
951 OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
952 OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
954 lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL;
956 l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
957 l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
958 l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
959 l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
961 if (l_tccp->numresolutions > *p_max_res) {
962 *p_max_res = l_tccp->numresolutions;
965 /* use custom size for precincts*/
966 l_level_no = l_tccp->numresolutions;
967 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
968 OPJ_UINT32 l_dx, l_dy;
972 /* precinct width and height*/
973 l_pdx = l_tccp->prcw[resno];
974 l_pdy = l_tccp->prch[resno];
975 if (lResolutionPtr) {
976 *lResolutionPtr++ = l_pdx;
977 *lResolutionPtr++ = l_pdy;
979 if (l_pdx + l_level_no < 32 &&
980 l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
981 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
982 /* take the minimum size for l_dx for each comp and resolution*/
983 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
985 if (l_pdy + l_level_no < 32 &&
986 l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
987 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
988 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
991 /* various calculations of extents*/
992 l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
993 l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
994 l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
995 l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
996 l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
997 l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
998 l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
999 py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1000 l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
1001 l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
1002 if (lResolutionPtr) {
1003 *lResolutionPtr++ = l_pw;
1004 *lResolutionPtr++ = l_ph;
1006 l_product = l_pw * l_ph;
1008 /* update precision*/
1009 if (l_product > *p_max_prec) {
1010 *p_max_prec = l_product;
1019 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
1022 opj_event_mgr_t* manager)
1025 OPJ_UINT32 pino, compno;
1026 /* number of poc in the p_pi*/
1027 OPJ_UINT32 l_poc_bound;
1029 /* pointers to tile coding parameters and components.*/
1030 opj_pi_iterator_t *l_pi = 00;
1031 opj_tcp_t *tcp = 00;
1032 const opj_tccp_t *tccp = 00;
1034 /* current packet iterator being allocated*/
1035 opj_pi_iterator_t *l_current_pi = 00;
1037 /* preconditions in debug*/
1039 assert(image != 00);
1040 assert(tileno < cp->tw * cp->th);
1042 /* initializations*/
1043 tcp = &cp->tcps[tileno];
1044 l_poc_bound = tcp->numpocs + 1;
1046 /* memory allocations*/
1047 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
1048 sizeof(opj_pi_iterator_t));
1053 l_current_pi = l_pi;
1054 for (pino = 0; pino < l_poc_bound ; ++pino) {
1056 l_current_pi->manager = manager;
1058 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
1059 sizeof(opj_pi_comp_t));
1060 if (! l_current_pi->comps) {
1061 opj_pi_destroy(l_pi, l_poc_bound);
1065 l_current_pi->numcomps = image->numcomps;
1067 for (compno = 0; compno < image->numcomps; ++compno) {
1068 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1070 tccp = &tcp->tccps[compno];
1072 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
1073 sizeof(opj_pi_resolution_t));
1074 if (!comp->resolutions) {
1075 opj_pi_destroy(l_pi, l_poc_bound);
1079 comp->numresolutions = tccp->numresolutions;
1086 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
1087 OPJ_UINT32 p_tileno,
1092 OPJ_UINT32 p_max_prec,
1093 OPJ_UINT32 p_max_res,
1094 OPJ_UINT32 p_dx_min,
1095 OPJ_UINT32 p_dy_min)
1099 /* tile coding parameter*/
1100 opj_tcp_t *l_tcp = 00;
1101 /* current poc being updated*/
1102 opj_poc_t * l_current_poc = 00;
1105 OPJ_UINT32 l_poc_bound;
1107 OPJ_ARG_NOT_USED(p_max_res);
1109 /* preconditions in debug*/
1111 assert(p_tileno < p_cp->tw * p_cp->th);
1113 /* initializations*/
1114 l_tcp = &p_cp->tcps [p_tileno];
1115 /* number of iterations in the loop */
1116 l_poc_bound = l_tcp->numpocs + 1;
1118 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1119 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1120 l_current_poc = l_tcp->pocs;
1122 l_current_poc->compS = l_current_poc->compno0;
1123 l_current_poc->compE = l_current_poc->compno1;
1124 l_current_poc->resS = l_current_poc->resno0;
1125 l_current_poc->resE = l_current_poc->resno1;
1126 l_current_poc->layE = l_current_poc->layno1;
1128 /* special treatment for the first element*/
1129 l_current_poc->layS = 0;
1130 l_current_poc->prg = l_current_poc->prg1;
1131 l_current_poc->prcS = 0;
1133 l_current_poc->prcE = p_max_prec;
1134 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1135 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1136 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1137 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1138 l_current_poc->dx = p_dx_min;
1139 l_current_poc->dy = p_dy_min;
1142 for (pino = 1; pino < l_poc_bound ; ++pino) {
1143 l_current_poc->compS = l_current_poc->compno0;
1144 l_current_poc->compE = l_current_poc->compno1;
1145 l_current_poc->resS = l_current_poc->resno0;
1146 l_current_poc->resE = l_current_poc->resno1;
1147 l_current_poc->layE = l_current_poc->layno1;
1148 l_current_poc->prg = l_current_poc->prg1;
1149 l_current_poc->prcS = 0;
1150 /* special treatment here different from the first element*/
1151 l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
1152 l_current_poc->layE : 0;
1154 l_current_poc->prcE = p_max_prec;
1155 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1156 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1157 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1158 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1159 l_current_poc->dx = p_dx_min;
1160 l_current_poc->dy = p_dy_min;
1165 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
1166 OPJ_UINT32 p_num_comps,
1167 OPJ_UINT32 p_tileno,
1172 OPJ_UINT32 p_max_prec,
1173 OPJ_UINT32 p_max_res,
1174 OPJ_UINT32 p_dx_min,
1175 OPJ_UINT32 p_dy_min)
1179 /* tile coding parameter*/
1180 opj_tcp_t *l_tcp = 00;
1181 /* current poc being updated*/
1182 opj_poc_t * l_current_poc = 00;
1184 OPJ_UINT32 l_poc_bound;
1186 /* preconditions in debug*/
1188 assert(p_tileno < p_cp->tw * p_cp->th);
1190 /* initializations*/
1191 l_tcp = &p_cp->tcps [p_tileno];
1193 /* number of iterations in the loop */
1194 l_poc_bound = l_tcp->numpocs + 1;
1196 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1197 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1198 l_current_poc = l_tcp->pocs;
1200 for (pino = 0; pino < l_poc_bound ; ++pino) {
1201 l_current_poc->compS = 0;
1202 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1203 l_current_poc->resS = 0;
1204 l_current_poc->resE = p_max_res;
1205 l_current_poc->layS = 0;
1206 l_current_poc->layE = l_tcp->numlayers;
1207 l_current_poc->prg = l_tcp->prg;
1208 l_current_poc->prcS = 0;
1209 l_current_poc->prcE = p_max_prec;
1210 l_current_poc->txS = p_tx0;
1211 l_current_poc->txE = p_tx1;
1212 l_current_poc->tyS = p_ty0;
1213 l_current_poc->tyE = p_ty1;
1214 l_current_poc->dx = p_dx_min;
1215 l_current_poc->dy = p_dy_min;
1220 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1222 OPJ_UINT32 p_max_precision,
1223 OPJ_UINT32 p_max_res)
1228 /* encoding parameters to set*/
1231 opj_pi_iterator_t * l_current_pi = 00;
1232 opj_poc_t* l_current_poc = 0;
1234 OPJ_ARG_NOT_USED(p_max_res);
1236 /* preconditions in debug*/
1238 assert(p_tcp != 00);
1240 /* initializations*/
1241 l_bound = p_tcp->numpocs + 1;
1242 l_current_pi = p_pi;
1243 l_current_poc = p_tcp->pocs;
1245 for (pino = 0; pino < l_bound; ++pino) {
1246 l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1247 l_current_pi->first = 1;
1249 l_current_pi->poc.resno0 =
1250 l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1251 l_current_pi->poc.compno0 =
1252 l_current_poc->compno0; /* Component Index #0 (Start) */
1253 l_current_pi->poc.layno0 = 0;
1254 l_current_pi->poc.precno0 = 0;
1255 l_current_pi->poc.resno1 =
1256 l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1257 l_current_pi->poc.compno1 =
1258 l_current_poc->compno1; /* Component Index #0 (End) */
1259 l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
1260 p_tcp->numlayers); /* Layer Index #0 (End) */
1261 l_current_pi->poc.precno1 = p_max_precision;
1267 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1269 OPJ_UINT32 p_max_precision,
1270 OPJ_UINT32 p_max_res)
1275 /* encoding parameters to set*/
1278 opj_pi_iterator_t * l_current_pi = 00;
1279 /* preconditions in debug*/
1280 assert(p_tcp != 00);
1283 /* initializations*/
1284 l_bound = p_tcp->numpocs + 1;
1285 l_current_pi = p_pi;
1287 for (pino = 0; pino < l_bound; ++pino) {
1288 l_current_pi->poc.prg = p_tcp->prg;
1289 l_current_pi->first = 1;
1290 l_current_pi->poc.resno0 = 0;
1291 l_current_pi->poc.compno0 = 0;
1292 l_current_pi->poc.layno0 = 0;
1293 l_current_pi->poc.precno0 = 0;
1294 l_current_pi->poc.resno1 = p_max_res;
1295 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1296 l_current_pi->poc.layno1 = p_tcp->numlayers;
1297 l_current_pi->poc.precno1 = p_max_precision;
1304 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1308 const OPJ_CHAR *prog)
1311 opj_tcp_t *tcps = &cp->tcps[tileno];
1312 opj_poc_t *tcp = &tcps->pocs[pino];
1315 for (i = pos; i >= 0; i--) {
1318 if (tcp->res_t == tcp->resE) {
1319 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1329 if (tcp->comp_t == tcp->compE) {
1330 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1340 if (tcp->lay_t == tcp->layE) {
1341 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1352 case OPJ_LRCP: /* fall through */
1354 if (tcp->prc_t == tcp->prcE) {
1355 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1365 if (tcp->tx0_t == tcp->txE) {
1367 if (tcp->ty0_t == tcp->tyE) {
1368 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1389 ==========================================================
1390 Packet iterator interface
1391 ==========================================================
1393 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1395 OPJ_UINT32 p_tile_no,
1396 opj_event_mgr_t* manager)
1398 OPJ_UINT32 numcomps = p_image->numcomps;
1402 OPJ_UINT32 compno, resno;
1404 /* to store w, h, dx and dy for all components and resolutions */
1405 OPJ_UINT32 * l_tmp_data;
1406 OPJ_UINT32 ** l_tmp_ptr;
1408 /* encoding parameters to set */
1409 OPJ_UINT32 l_max_res;
1410 OPJ_UINT32 l_max_prec;
1411 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1412 OPJ_UINT32 l_dx_min, l_dy_min;
1414 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1415 OPJ_UINT32 l_data_stride;
1418 opj_pi_iterator_t *l_pi = 00;
1419 opj_tcp_t *l_tcp = 00;
1420 const opj_tccp_t *l_tccp = 00;
1421 opj_pi_comp_t *l_current_comp = 00;
1422 opj_image_comp_t * l_img_comp = 00;
1423 opj_pi_iterator_t * l_current_pi = 00;
1424 OPJ_UINT32 * l_encoding_value_ptr = 00;
1426 /* preconditions in debug */
1428 assert(p_image != 00);
1429 assert(p_tile_no < p_cp->tw * p_cp->th);
1431 /* initializations */
1432 l_tcp = &p_cp->tcps[p_tile_no];
1433 l_bound = l_tcp->numpocs + 1;
1435 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1436 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1437 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1442 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1443 numcomps * sizeof(OPJ_UINT32 *));
1446 opj_free(l_tmp_data);
1450 /* memory allocation for pi */
1451 l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
1453 opj_free(l_tmp_data);
1454 opj_free(l_tmp_ptr);
1458 l_encoding_value_ptr = l_tmp_data;
1459 /* update pointer array */
1461 (compno = 0; compno < numcomps; ++compno) {
1462 l_tmp_ptr[compno] = l_encoding_value_ptr;
1463 l_encoding_value_ptr += l_data_stride;
1465 /* get encoding parameters */
1466 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1467 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1469 /* step calculations */
1471 l_step_c = l_max_prec * l_step_p;
1472 l_step_r = numcomps * l_step_c;
1473 l_step_l = l_max_res * l_step_r;
1475 /* set values for first packet iterator */
1476 l_current_pi = l_pi;
1478 /* memory allocation for include */
1479 /* prevent an integer overflow issue */
1480 /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1481 l_current_pi->include = 00;
1482 if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
1483 l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
1484 l_current_pi->include = (OPJ_INT16*) opj_calloc(
1485 l_current_pi->include_size, sizeof(OPJ_INT16));
1488 if (!l_current_pi->include) {
1489 opj_free(l_tmp_data);
1490 opj_free(l_tmp_ptr);
1491 opj_pi_destroy(l_pi, l_bound);
1495 /* special treatment for the first packet iterator */
1496 l_current_comp = l_current_pi->comps;
1497 l_img_comp = p_image->comps;
1498 l_tccp = l_tcp->tccps;
1500 l_current_pi->tx0 = l_tx0;
1501 l_current_pi->ty0 = l_ty0;
1502 l_current_pi->tx1 = l_tx1;
1503 l_current_pi->ty1 = l_ty1;
1505 /*l_current_pi->dx = l_img_comp->dx;*/
1506 /*l_current_pi->dy = l_img_comp->dy;*/
1508 l_current_pi->step_p = l_step_p;
1509 l_current_pi->step_c = l_step_c;
1510 l_current_pi->step_r = l_step_r;
1511 l_current_pi->step_l = l_step_l;
1513 /* allocation for components and number of components has already been calculated by opj_pi_create */
1515 (compno = 0; compno < numcomps; ++compno) {
1516 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1517 l_encoding_value_ptr = l_tmp_ptr[compno];
1519 l_current_comp->dx = l_img_comp->dx;
1520 l_current_comp->dy = l_img_comp->dy;
1521 /* resolutions have already been initialized */
1523 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1524 l_res->pdx = *(l_encoding_value_ptr++);
1525 l_res->pdy = *(l_encoding_value_ptr++);
1526 l_res->pw = *(l_encoding_value_ptr++);
1527 l_res->ph = *(l_encoding_value_ptr++);
1536 for (pino = 1 ; pino < l_bound ; ++pino) {
1537 l_current_comp = l_current_pi->comps;
1538 l_img_comp = p_image->comps;
1539 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 */
1554 (compno = 0; compno < numcomps; ++compno) {
1555 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1556 l_encoding_value_ptr = l_tmp_ptr[compno];
1558 l_current_comp->dx = l_img_comp->dx;
1559 l_current_comp->dy = l_img_comp->dy;
1560 /* resolutions have already been initialized */
1562 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1563 l_res->pdx = *(l_encoding_value_ptr++);
1564 l_res->pdy = *(l_encoding_value_ptr++);
1565 l_res->pw = *(l_encoding_value_ptr++);
1566 l_res->ph = *(l_encoding_value_ptr++);
1573 /* special treatment*/
1574 l_current_pi->include = (l_current_pi - 1)->include;
1575 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1578 opj_free(l_tmp_data);
1580 opj_free(l_tmp_ptr);
1584 opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1586 opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1592 OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image,
1593 const opj_cp_t *p_cp,
1594 OPJ_UINT32 p_tile_no)
1596 OPJ_UINT32 l_max_res;
1597 OPJ_UINT32 l_max_prec;
1598 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1599 OPJ_UINT32 l_dx_min, l_dy_min;
1601 /* preconditions in debug*/
1603 assert(p_image != 00);
1604 assert(p_tile_no < p_cp->tw * p_cp->th);
1606 /* get encoding parameters*/
1607 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1608 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL);
1610 return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps *
1615 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1617 OPJ_UINT32 p_tile_no,
1618 J2K_T2_MODE p_t2_mode,
1619 opj_event_mgr_t* manager)
1621 OPJ_UINT32 numcomps = p_image->numcomps;
1625 OPJ_UINT32 compno, resno;
1627 /* to store w, h, dx and dy for all components and resolutions*/
1628 OPJ_UINT32 * l_tmp_data;
1629 OPJ_UINT32 ** l_tmp_ptr;
1631 /* encoding parameters to set*/
1632 OPJ_UINT32 l_max_res;
1633 OPJ_UINT32 l_max_prec;
1634 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1635 OPJ_UINT32 l_dx_min, l_dy_min;
1637 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1638 OPJ_UINT32 l_data_stride;
1641 opj_pi_iterator_t *l_pi = 00;
1642 opj_tcp_t *l_tcp = 00;
1643 const opj_tccp_t *l_tccp = 00;
1644 opj_pi_comp_t *l_current_comp = 00;
1645 opj_image_comp_t * l_img_comp = 00;
1646 opj_pi_iterator_t * l_current_pi = 00;
1647 OPJ_UINT32 * l_encoding_value_ptr = 00;
1649 /* preconditions in debug*/
1651 assert(p_image != 00);
1652 assert(p_tile_no < p_cp->tw * p_cp->th);
1654 /* initializations*/
1655 l_tcp = &p_cp->tcps[p_tile_no];
1656 l_bound = l_tcp->numpocs + 1;
1658 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1659 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1660 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1665 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1666 numcomps * sizeof(OPJ_UINT32 *));
1668 opj_free(l_tmp_data);
1672 /* memory allocation for pi*/
1673 l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
1675 opj_free(l_tmp_data);
1676 opj_free(l_tmp_ptr);
1680 l_encoding_value_ptr = l_tmp_data;
1681 /* update pointer array*/
1682 for (compno = 0; compno < numcomps; ++compno) {
1683 l_tmp_ptr[compno] = l_encoding_value_ptr;
1684 l_encoding_value_ptr += l_data_stride;
1687 /* get encoding parameters*/
1688 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1689 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1691 /* step calculations*/
1693 l_step_c = l_max_prec * l_step_p;
1694 l_step_r = numcomps * l_step_c;
1695 l_step_l = l_max_res * l_step_r;
1697 /* set values for first packet iterator*/
1698 l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1699 l_current_pi = l_pi;
1701 /* memory allocation for include*/
1702 l_current_pi->include_size = l_tcp->numlayers * l_step_l;
1703 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
1705 if (!l_current_pi->include) {
1706 opj_free(l_tmp_data);
1707 opj_free(l_tmp_ptr);
1708 opj_pi_destroy(l_pi, l_bound);
1712 /* special treatment for the first packet iterator*/
1713 l_current_comp = l_current_pi->comps;
1714 l_img_comp = p_image->comps;
1715 l_tccp = l_tcp->tccps;
1716 l_current_pi->tx0 = l_tx0;
1717 l_current_pi->ty0 = l_ty0;
1718 l_current_pi->tx1 = l_tx1;
1719 l_current_pi->ty1 = l_ty1;
1720 l_current_pi->dx = l_dx_min;
1721 l_current_pi->dy = l_dy_min;
1722 l_current_pi->step_p = l_step_p;
1723 l_current_pi->step_c = l_step_c;
1724 l_current_pi->step_r = l_step_r;
1725 l_current_pi->step_l = l_step_l;
1727 /* allocation for components and number of components has already been calculated by opj_pi_create */
1728 for (compno = 0; compno < numcomps; ++compno) {
1729 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1730 l_encoding_value_ptr = l_tmp_ptr[compno];
1732 l_current_comp->dx = l_img_comp->dx;
1733 l_current_comp->dy = l_img_comp->dy;
1735 /* resolutions have already been initialized */
1736 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1737 l_res->pdx = *(l_encoding_value_ptr++);
1738 l_res->pdy = *(l_encoding_value_ptr++);
1739 l_res->pw = *(l_encoding_value_ptr++);
1740 l_res->ph = *(l_encoding_value_ptr++);
1750 for (pino = 1 ; pino < l_bound ; ++pino) {
1751 l_current_comp = l_current_pi->comps;
1752 l_img_comp = p_image->comps;
1753 l_tccp = l_tcp->tccps;
1755 l_current_pi->tx0 = l_tx0;
1756 l_current_pi->ty0 = l_ty0;
1757 l_current_pi->tx1 = l_tx1;
1758 l_current_pi->ty1 = l_ty1;
1759 l_current_pi->dx = l_dx_min;
1760 l_current_pi->dy = l_dy_min;
1761 l_current_pi->step_p = l_step_p;
1762 l_current_pi->step_c = l_step_c;
1763 l_current_pi->step_r = l_step_r;
1764 l_current_pi->step_l = l_step_l;
1766 /* allocation for components and number of components has already been calculated by opj_pi_create */
1767 for (compno = 0; compno < numcomps; ++compno) {
1768 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1769 l_encoding_value_ptr = l_tmp_ptr[compno];
1771 l_current_comp->dx = l_img_comp->dx;
1772 l_current_comp->dy = l_img_comp->dy;
1773 /* resolutions have already been initialized */
1774 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1775 l_res->pdx = *(l_encoding_value_ptr++);
1776 l_res->pdy = *(l_encoding_value_ptr++);
1777 l_res->pw = *(l_encoding_value_ptr++);
1778 l_res->ph = *(l_encoding_value_ptr++);
1786 /* special treatment*/
1787 l_current_pi->include = (l_current_pi - 1)->include;
1788 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1792 opj_free(l_tmp_data);
1794 opj_free(l_tmp_ptr);
1797 if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1798 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1799 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1801 opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
1802 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1808 void opj_pi_create_encode(opj_pi_iterator_t *pi,
1814 J2K_T2_MODE t2_mode)
1816 const OPJ_CHAR *prog;
1818 OPJ_UINT32 incr_top = 1, resetX = 0;
1819 opj_tcp_t *tcps = &cp->tcps[tileno];
1820 opj_poc_t *tcp = &tcps->pocs[pino];
1822 prog = opj_j2k_convert_progression_order(tcp->prg);
1825 pi[pino].poc.prg = tcp->prg;
1827 if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1828 !OPJ_IS_IMF(cp->rsiz) &&
1829 (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) {
1830 pi[pino].poc.resno0 = tcp->resS;
1831 pi[pino].poc.resno1 = tcp->resE;
1832 pi[pino].poc.compno0 = tcp->compS;
1833 pi[pino].poc.compno1 = tcp->compE;
1834 pi[pino].poc.layno0 = tcp->layS;
1835 pi[pino].poc.layno1 = tcp->layE;
1836 pi[pino].poc.precno0 = tcp->prcS;
1837 pi[pino].poc.precno1 = tcp->prcE;
1838 pi[pino].poc.tx0 = tcp->txS;
1839 pi[pino].poc.ty0 = tcp->tyS;
1840 pi[pino].poc.tx1 = tcp->txE;
1841 pi[pino].poc.ty1 = tcp->tyE;
1843 for (i = tppos + 1; i < 4; i++) {
1846 pi[pino].poc.resno0 = tcp->resS;
1847 pi[pino].poc.resno1 = tcp->resE;
1850 pi[pino].poc.compno0 = tcp->compS;
1851 pi[pino].poc.compno1 = tcp->compE;
1854 pi[pino].poc.layno0 = tcp->layS;
1855 pi[pino].poc.layno1 = tcp->layE;
1861 pi[pino].poc.precno0 = tcp->prcS;
1862 pi[pino].poc.precno1 = tcp->prcE;
1865 pi[pino].poc.tx0 = tcp->txS;
1866 pi[pino].poc.ty0 = tcp->tyS;
1867 pi[pino].poc.tx1 = tcp->txE;
1868 pi[pino].poc.ty1 = tcp->tyE;
1876 for (i = tppos; i >= 0; i--) {
1879 tcp->comp_t = tcp->compS;
1880 pi[pino].poc.compno0 = tcp->comp_t;
1881 pi[pino].poc.compno1 = tcp->comp_t + 1;
1885 tcp->res_t = tcp->resS;
1886 pi[pino].poc.resno0 = tcp->res_t;
1887 pi[pino].poc.resno1 = tcp->res_t + 1;
1891 tcp->lay_t = tcp->layS;
1892 pi[pino].poc.layno0 = tcp->lay_t;
1893 pi[pino].poc.layno1 = tcp->lay_t + 1;
1900 tcp->prc_t = tcp->prcS;
1901 pi[pino].poc.precno0 = tcp->prc_t;
1902 pi[pino].poc.precno1 = tcp->prc_t + 1;
1906 tcp->tx0_t = tcp->txS;
1907 tcp->ty0_t = tcp->tyS;
1908 pi[pino].poc.tx0 = tcp->tx0_t;
1909 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1910 pi[pino].poc.ty0 = tcp->ty0_t;
1911 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1912 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1913 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1921 for (i = tppos; i >= 0; i--) {
1924 pi[pino].poc.compno0 = tcp->comp_t - 1;
1925 pi[pino].poc.compno1 = tcp->comp_t;
1928 pi[pino].poc.resno0 = tcp->res_t - 1;
1929 pi[pino].poc.resno1 = tcp->res_t;
1932 pi[pino].poc.layno0 = tcp->lay_t - 1;
1933 pi[pino].poc.layno1 = tcp->lay_t;
1939 pi[pino].poc.precno0 = tcp->prc_t - 1;
1940 pi[pino].poc.precno1 = tcp->prc_t;
1943 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1944 pi[pino].poc.tx1 = tcp->tx0_t ;
1945 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1946 pi[pino].poc.ty1 = tcp->ty0_t ;
1951 if (incr_top == 1) {
1954 if (tcp->res_t == tcp->resE) {
1955 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1956 tcp->res_t = tcp->resS;
1957 pi[pino].poc.resno0 = tcp->res_t;
1958 pi[pino].poc.resno1 = tcp->res_t + 1;
1965 pi[pino].poc.resno0 = tcp->res_t;
1966 pi[pino].poc.resno1 = tcp->res_t + 1;
1972 if (tcp->comp_t == tcp->compE) {
1973 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1974 tcp->comp_t = tcp->compS;
1975 pi[pino].poc.compno0 = tcp->comp_t;
1976 pi[pino].poc.compno1 = tcp->comp_t + 1;
1983 pi[pino].poc.compno0 = tcp->comp_t;
1984 pi[pino].poc.compno1 = tcp->comp_t + 1;
1990 if (tcp->lay_t == tcp->layE) {
1991 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1992 tcp->lay_t = tcp->layS;
1993 pi[pino].poc.layno0 = tcp->lay_t;
1994 pi[pino].poc.layno1 = tcp->lay_t + 1;
2001 pi[pino].poc.layno0 = tcp->lay_t;
2002 pi[pino].poc.layno1 = tcp->lay_t + 1;
2011 if (tcp->prc_t == tcp->prcE) {
2012 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2013 tcp->prc_t = tcp->prcS;
2014 pi[pino].poc.precno0 = tcp->prc_t;
2015 pi[pino].poc.precno1 = tcp->prc_t + 1;
2022 pi[pino].poc.precno0 = tcp->prc_t;
2023 pi[pino].poc.precno1 = tcp->prc_t + 1;
2029 if (tcp->tx0_t >= tcp->txE) {
2030 if (tcp->ty0_t >= tcp->tyE) {
2031 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2032 tcp->ty0_t = tcp->tyS;
2033 pi[pino].poc.ty0 = tcp->ty0_t;
2034 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2035 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2043 pi[pino].poc.ty0 = tcp->ty0_t;
2044 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2045 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2050 tcp->tx0_t = tcp->txS;
2051 pi[pino].poc.tx0 = tcp->tx0_t;
2052 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2053 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2056 pi[pino].poc.tx0 = tcp->tx0_t;
2057 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2058 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2071 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
2072 OPJ_UINT32 p_nb_elements)
2074 OPJ_UINT32 compno, pino;
2075 opj_pi_iterator_t *l_current_pi = p_pi;
2077 if (p_pi->include) {
2078 opj_free(p_pi->include);
2081 for (pino = 0; pino < p_nb_elements; ++pino) {
2082 if (l_current_pi->comps) {
2083 opj_pi_comp_t *l_current_component = l_current_pi->comps;
2084 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
2085 if (l_current_component->resolutions) {
2086 opj_free(l_current_component->resolutions);
2087 l_current_component->resolutions = 00;
2090 ++l_current_component;
2092 opj_free(l_current_pi->comps);
2093 l_current_pi->comps = 0;
2103 void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
2105 OPJ_UINT32 p_tile_no)
2107 /* encoding parameters to set */
2108 OPJ_UINT32 l_max_res;
2109 OPJ_UINT32 l_max_prec;
2110 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
2111 OPJ_UINT32 l_dx_min, l_dy_min;
2114 opj_tcp_t *l_tcp = 00;
2118 assert(p_image != 00);
2119 assert(p_tile_no < p_cp->tw * p_cp->th);
2121 l_tcp = &(p_cp->tcps[p_tile_no]);
2123 /* get encoding parameters */
2124 opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
2125 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
2128 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
2129 l_max_prec, l_max_res, l_dx_min, l_dy_min);
2131 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
2132 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
2136 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
2138 switch (pi->poc.prg) {
2140 return opj_pi_next_lrcp(pi);
2142 return opj_pi_next_rlcp(pi);
2144 return opj_pi_next_rpcl(pi);
2146 return opj_pi_next_pcrl(pi);
2148 return opj_pi_next_cprl(pi);
2149 case OPJ_PROG_UNKNOWN: