blob: ad96f47eb50c5ba050448ac7262888b43adecb77 [file] [log] [blame]
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001/*
Nicolas Pena088ca032017-09-01 13:25:16 -04002 * 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
John Abd-El-Malek5110c472014-05-17 22:33:34 -07004 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
6 *
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
Nicolas Pena088ca032017-09-01 13:25:16 -040011 * Copyright (c) 2003-2007, Francois-Olivier Devaux
John Abd-El-Malek5110c472014-05-17 22:33:34 -070012 * 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.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
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.
25 *
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.
37 */
38
Daniel Hosseiniana81ff722021-02-17 20:56:08 +000039#define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_UINT32
40
John Abd-El-Malek5110c472014-05-17 22:33:34 -070041#include "opj_includes.h"
42
43/** @defgroup PI PI - Implementation of a packet iterator */
44/*@{*/
45
46/** @name Local static functions */
47/*@{*/
48
49/**
50Get 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
53*/
54static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
55/**
56Get 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
59*/
60static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
61/**
62Get 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
65*/
66static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
67/**
68Get 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
71*/
72static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
73/**
74Get 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
77*/
78static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
79
80/**
81 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
82 *
Nicolas Pena088ca032017-09-01 13:25:16 -040083 * @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.
John Abd-El-Malek5110c472014-05-17 22:33:34 -070093 */
Nicolas Pena088ca032017-09-01 13:25:16 -040094static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
95 OPJ_UINT32 p_tileno,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +000096 OPJ_UINT32 p_tx0,
97 OPJ_UINT32 p_tx1,
98 OPJ_UINT32 p_ty0,
99 OPJ_UINT32 p_ty1,
Nicolas Pena088ca032017-09-01 13:25:16 -0400100 OPJ_UINT32 p_max_prec,
101 OPJ_UINT32 p_max_res,
102 OPJ_UINT32 p_dx_min,
103 OPJ_UINT32 p_dy_min);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700104
105/**
106 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
107 *
Nicolas Pena088ca032017-09-01 13:25:16 -0400108 * @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.
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700119 */
Nicolas Pena088ca032017-09-01 13:25:16 -0400120static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
121 OPJ_UINT32 p_num_comps,
122 OPJ_UINT32 p_tileno,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000123 OPJ_UINT32 p_tx0,
124 OPJ_UINT32 p_tx1,
125 OPJ_UINT32 p_ty0,
126 OPJ_UINT32 p_ty1,
Nicolas Pena088ca032017-09-01 13:25:16 -0400127 OPJ_UINT32 p_max_prec,
128 OPJ_UINT32 p_max_res,
129 OPJ_UINT32 p_dx_min,
130 OPJ_UINT32 p_dy_min);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700131/**
132 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
Nicolas Pena088ca032017-09-01 13:25:16 -0400133 *
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.
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700145 */
146static void opj_get_encoding_parameters(const opj_image_t *p_image,
147 const opj_cp_t *p_cp,
148 OPJ_UINT32 tileno,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000149 OPJ_UINT32 * p_tx0,
150 OPJ_UINT32 * p_tx1,
151 OPJ_UINT32 * p_ty0,
152 OPJ_UINT32 * p_ty1,
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700153 OPJ_UINT32 * p_dx_min,
154 OPJ_UINT32 * p_dy_min,
155 OPJ_UINT32 * p_max_prec,
Nicolas Pena088ca032017-09-01 13:25:16 -0400156 OPJ_UINT32 * p_max_res);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700157
158/**
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 , ...
164 *
Nicolas Pena088ca032017-09-01 13:25:16 -0400165 * @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.
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700177 */
178static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
Nicolas Pena088ca032017-09-01 13:25:16 -0400179 const opj_cp_t *p_cp,
180 OPJ_UINT32 tileno,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000181 OPJ_UINT32 * p_tx0,
182 OPJ_UINT32 * p_tx1,
183 OPJ_UINT32 * p_ty0,
184 OPJ_UINT32 * p_ty1,
Nicolas Pena088ca032017-09-01 13:25:16 -0400185 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);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700190/**
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.
Nicolas Pena088ca032017-09-01 13:25:16 -0400193 *
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.
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000197 * @param manager Event manager
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700198 */
Nicolas Pena088ca032017-09-01 13:25:16 -0400199static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
200 const opj_cp_t *p_cp,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000201 OPJ_UINT32 tileno,
202 opj_event_mgr_t* manager);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700203/**
204 * FIXME DOC
205 */
Nicolas Pena088ca032017-09-01 13:25:16 -0400206static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
207 opj_tcp_t * p_tcp,
208 OPJ_UINT32 p_max_precision,
209 OPJ_UINT32 p_max_res);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700210/**
211 * FIXME DOC
212 */
Nicolas Pena088ca032017-09-01 13:25:16 -0400213static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
214 opj_tcp_t * p_tcp,
215 OPJ_UINT32 p_max_precision,
216 OPJ_UINT32 p_max_res);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700217
218/**
219 * FIXME DOC
220 */
Nicolas Pena088ca032017-09-01 13:25:16 -0400221static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
222 opj_cp_t *cp,
223 OPJ_UINT32 tileno,
224 OPJ_UINT32 pino,
225 const OPJ_CHAR *prog);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700226
227/*@}*/
228
229/*@}*/
230
231/*
232==========================================================
233 local functions
234==========================================================
235*/
236
Nicolas Pena088ca032017-09-01 13:25:16 -0400237static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
238{
239 opj_pi_comp_t *comp = NULL;
240 opj_pi_resolution_t *res = NULL;
241 OPJ_UINT32 index = 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700242
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000243 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");
247 return OPJ_FALSE;
248 }
249
Nicolas Pena088ca032017-09-01 13:25:16 -0400250 if (!pi->first) {
251 comp = &pi->comps[pi->compno];
252 res = &comp->resolutions[pi->resno];
253 goto LABEL_SKIP;
254 } else {
255 pi->first = 0;
256 }
257
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;
260 pi->resno++) {
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) {
264 continue;
265 }
266 res = &comp->resolutions[pi->resno];
267 if (!pi->tp_on) {
268 pi->poc.precno1 = res->pw * res->ph;
269 }
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) {
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000280 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
Nicolas Pena088ca032017-09-01 13:25:16 -0400281 return OPJ_FALSE;
282 }
283 if (!pi->include[index]) {
284 pi->include[index] = 1;
285 return OPJ_TRUE;
286 }
287LABEL_SKIP:
288 ;
289 }
290 }
291 }
292 }
293
294 return OPJ_FALSE;
295}
296
297static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
298{
299 opj_pi_comp_t *comp = NULL;
300 opj_pi_resolution_t *res = NULL;
301 OPJ_UINT32 index = 0;
302
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000303 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");
307 return OPJ_FALSE;
308 }
309
Nicolas Pena088ca032017-09-01 13:25:16 -0400310 if (!pi->first) {
311 comp = &pi->comps[pi->compno];
312 res = &comp->resolutions[pi->resno];
313 goto LABEL_SKIP;
314 } else {
315 pi->first = 0;
316 }
317
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) {
323 continue;
324 }
325 res = &comp->resolutions[pi->resno];
326 if (!pi->tp_on) {
327 pi->poc.precno1 = res->pw * res->ph;
328 }
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) {
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000333 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
Nicolas Pena088ca032017-09-01 13:25:16 -0400334 return OPJ_FALSE;
335 }
336 if (!pi->include[index]) {
337 pi->include[index] = 1;
338 return OPJ_TRUE;
339 }
340LABEL_SKIP:
341 ;
342 }
343 }
344 }
345 }
346
347 return OPJ_FALSE;
348}
349
350static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
351{
352 opj_pi_comp_t *comp = NULL;
353 opj_pi_resolution_t *res = NULL;
354 OPJ_UINT32 index = 0;
355
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000356 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");
360 return OPJ_FALSE;
361 }
362
Nicolas Pena088ca032017-09-01 13:25:16 -0400363 if (!pi->first) {
364 goto LABEL_SKIP;
365 } else {
366 OPJ_UINT32 compno, resno;
367 pi->first = 0;
368 pi->dx = 0;
369 pi->dy = 0;
370 for (compno = 0; compno < pi->numcomps; compno++) {
371 comp = &pi->comps[compno];
372 for (resno = 0; resno < comp->numresolutions; resno++) {
373 OPJ_UINT32 dx, dy;
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);
379 }
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);
384 }
385 }
386 }
387 if (pi->dx == 0 || pi->dy == 0) {
388 return OPJ_FALSE;
389 }
390 }
391 if (!pi->tp_on) {
392 pi->poc.ty0 = pi->ty0;
393 pi->poc.tx0 = pi->tx0;
394 pi->poc.ty1 = pi->ty1;
395 pi->poc.tx1 = pi->tx1;
396 }
397 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000398 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))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400402 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
403 OPJ_UINT32 levelno;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000404 OPJ_UINT32 trx0, try0;
405 OPJ_UINT32 trx1, try1;
Nicolas Pena088ca032017-09-01 13:25:16 -0400406 OPJ_UINT32 rpx, rpy;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000407 OPJ_UINT32 prci, prcj;
Nicolas Pena088ca032017-09-01 13:25:16 -0400408 comp = &pi->comps[pi->compno];
409 if (pi->resno >= comp->numresolutions) {
410 continue;
411 }
412 res = &comp->resolutions[pi->resno];
413 levelno = comp->numresolutions - 1 - pi->resno;
Lei Zhangee35a502024-05-30 17:31:47 +0000414
415 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
416 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400417 continue;
418 }
Lei Zhangee35a502024-05-30 17:31:47 +0000419
420 trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
421 ((OPJ_UINT64)comp->dx << levelno));
422 try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
423 ((OPJ_UINT64)comp->dy << levelno));
424 trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
425 ((OPJ_UINT64)comp->dx << levelno));
426 try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
427 ((OPJ_UINT64)comp->dy << levelno));
Nicolas Pena088ca032017-09-01 13:25:16 -0400428 rpx = res->pdx + levelno;
429 rpy = res->pdy + levelno;
430
Lei Zhangee35a502024-05-30 17:31:47 +0000431 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
432 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400433 continue;
434 }
435
436 /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
Lei Zhangee35a502024-05-30 17:31:47 +0000437 if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
438 ((pi->y == pi->ty0) &&
439 (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400440 continue;
441 }
Lei Zhangee35a502024-05-30 17:31:47 +0000442 if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
443 ((pi->x == pi->tx0) &&
444 (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400445 continue;
446 }
447
448 if ((res->pw == 0) || (res->ph == 0)) {
449 continue;
450 }
451
452 if ((trx0 == trx1) || (try0 == try1)) {
453 continue;
454 }
455
Lei Zhangee35a502024-05-30 17:31:47 +0000456 prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
457 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000458 - opj_uint_floordivpow2(trx0, res->pdx);
Lei Zhangee35a502024-05-30 17:31:47 +0000459 prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
460 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000461 - opj_uint_floordivpow2(try0, res->pdy);
462 pi->precno = prci + prcj * res->pw;
Nicolas Pena088ca032017-09-01 13:25:16 -0400463 if (pi->precno >= res->pw * res->ph) {
464 return OPJ_FALSE;
465 }
466 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
467 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
468 pi->step_c + pi->precno * pi->step_p;
469 if (index >= pi->include_size) {
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000470 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
Nicolas Pena088ca032017-09-01 13:25:16 -0400471 return OPJ_FALSE;
472 }
473 if (!pi->include[index]) {
474 pi->include[index] = 1;
475 return OPJ_TRUE;
476 }
477LABEL_SKIP:
478 ;
479 }
480 }
481 }
482 }
483 }
484
485 return OPJ_FALSE;
486}
487
488static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
489{
490 opj_pi_comp_t *comp = NULL;
491 opj_pi_resolution_t *res = NULL;
492 OPJ_UINT32 index = 0;
493
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000494 if (pi->poc.compno0 >= pi->numcomps ||
495 pi->poc.compno1 >= pi->numcomps + 1) {
496 opj_event_msg(pi->manager, EVT_ERROR,
497 "opj_pi_next_pcrl(): invalid compno0/compno1\n");
498 return OPJ_FALSE;
499 }
500
Nicolas Pena088ca032017-09-01 13:25:16 -0400501 if (!pi->first) {
502 comp = &pi->comps[pi->compno];
503 goto LABEL_SKIP;
504 } else {
505 OPJ_UINT32 compno, resno;
506 pi->first = 0;
507 pi->dx = 0;
508 pi->dy = 0;
509 for (compno = 0; compno < pi->numcomps; compno++) {
510 comp = &pi->comps[compno];
511 for (resno = 0; resno < comp->numresolutions; resno++) {
512 OPJ_UINT32 dx, dy;
513 res = &comp->resolutions[resno];
514 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
515 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
516 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
517 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
518 }
519 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
520 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
521 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
522 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
523 }
524 }
525 }
526 if (pi->dx == 0 || pi->dy == 0) {
527 return OPJ_FALSE;
528 }
529 }
530 if (!pi->tp_on) {
531 pi->poc.ty0 = pi->ty0;
532 pi->poc.tx0 = pi->tx0;
533 pi->poc.ty1 = pi->ty1;
534 pi->poc.tx1 = pi->tx1;
535 }
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000536 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
537 pi->y += (pi->dy - (pi->y % pi->dy))) {
538 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
539 pi->x += (pi->dx - (pi->x % pi->dx))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400540 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
541 comp = &pi->comps[pi->compno];
542 for (pi->resno = pi->poc.resno0;
543 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
544 OPJ_UINT32 levelno;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000545 OPJ_UINT32 trx0, try0;
546 OPJ_UINT32 trx1, try1;
Nicolas Pena088ca032017-09-01 13:25:16 -0400547 OPJ_UINT32 rpx, rpy;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000548 OPJ_UINT32 prci, prcj;
Nicolas Pena088ca032017-09-01 13:25:16 -0400549 res = &comp->resolutions[pi->resno];
550 levelno = comp->numresolutions - 1 - pi->resno;
Lei Zhangee35a502024-05-30 17:31:47 +0000551
552 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
553 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400554 continue;
555 }
Lei Zhangee35a502024-05-30 17:31:47 +0000556
557 trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
558 ((OPJ_UINT64)comp->dx << levelno));
559 try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
560 ((OPJ_UINT64)comp->dy << levelno));
561 trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
562 ((OPJ_UINT64)comp->dx << levelno));
563 try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
564 ((OPJ_UINT64)comp->dy << levelno));
Nicolas Pena088ca032017-09-01 13:25:16 -0400565 rpx = res->pdx + levelno;
566 rpy = res->pdy + levelno;
567
Lei Zhangee35a502024-05-30 17:31:47 +0000568 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
569 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400570 continue;
571 }
572
573 /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
Lei Zhangee35a502024-05-30 17:31:47 +0000574 if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
575 ((pi->y == pi->ty0) &&
576 (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400577 continue;
578 }
Lei Zhangee35a502024-05-30 17:31:47 +0000579 if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
580 ((pi->x == pi->tx0) &&
581 (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400582 continue;
583 }
584
585 if ((res->pw == 0) || (res->ph == 0)) {
586 continue;
587 }
588
589 if ((trx0 == trx1) || (try0 == try1)) {
590 continue;
591 }
592
Lei Zhangee35a502024-05-30 17:31:47 +0000593 prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
594 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000595 - opj_uint_floordivpow2(trx0, res->pdx);
Lei Zhangee35a502024-05-30 17:31:47 +0000596 prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
597 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000598 - opj_uint_floordivpow2(try0, res->pdy);
599 pi->precno = prci + prcj * res->pw;
Nicolas Pena088ca032017-09-01 13:25:16 -0400600 if (pi->precno >= res->pw * res->ph) {
601 return OPJ_FALSE;
602 }
603 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
604 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
605 pi->step_c + pi->precno * pi->step_p;
606 if (index >= pi->include_size) {
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000607 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
Nicolas Pena088ca032017-09-01 13:25:16 -0400608 return OPJ_FALSE;
609 }
610 if (!pi->include[index]) {
611 pi->include[index] = 1;
612 return OPJ_TRUE;
613 }
614LABEL_SKIP:
615 ;
616 }
617 }
618 }
619 }
620 }
621
622 return OPJ_FALSE;
623}
624
625static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
626{
627 opj_pi_comp_t *comp = NULL;
628 opj_pi_resolution_t *res = NULL;
629 OPJ_UINT32 index = 0;
630
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000631 if (pi->poc.compno0 >= pi->numcomps ||
632 pi->poc.compno1 >= pi->numcomps + 1) {
633 opj_event_msg(pi->manager, EVT_ERROR,
634 "opj_pi_next_cprl(): invalid compno0/compno1\n");
635 return OPJ_FALSE;
636 }
637
Nicolas Pena088ca032017-09-01 13:25:16 -0400638 if (!pi->first) {
639 comp = &pi->comps[pi->compno];
640 goto LABEL_SKIP;
641 } else {
642 pi->first = 0;
643 }
644
645 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
646 OPJ_UINT32 resno;
647 comp = &pi->comps[pi->compno];
648 pi->dx = 0;
649 pi->dy = 0;
650 for (resno = 0; resno < comp->numresolutions; resno++) {
651 OPJ_UINT32 dx, dy;
652 res = &comp->resolutions[resno];
653 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
654 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
655 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
656 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
657 }
658 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
659 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
660 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
661 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
662 }
663 }
664 if (pi->dx == 0 || pi->dy == 0) {
665 return OPJ_FALSE;
666 }
667 if (!pi->tp_on) {
668 pi->poc.ty0 = pi->ty0;
669 pi->poc.tx0 = pi->tx0;
670 pi->poc.ty1 = pi->ty1;
671 pi->poc.tx1 = pi->tx1;
672 }
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000673 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
674 pi->y += (pi->dy - (pi->y % pi->dy))) {
675 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
676 pi->x += (pi->dx - (pi->x % pi->dx))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400677 for (pi->resno = pi->poc.resno0;
678 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
679 OPJ_UINT32 levelno;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000680 OPJ_UINT32 trx0, try0;
681 OPJ_UINT32 trx1, try1;
Nicolas Pena088ca032017-09-01 13:25:16 -0400682 OPJ_UINT32 rpx, rpy;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000683 OPJ_UINT32 prci, prcj;
Nicolas Pena088ca032017-09-01 13:25:16 -0400684 res = &comp->resolutions[pi->resno];
685 levelno = comp->numresolutions - 1 - pi->resno;
Lei Zhangee35a502024-05-30 17:31:47 +0000686
687 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
688 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400689 continue;
690 }
Lei Zhangee35a502024-05-30 17:31:47 +0000691
692 trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
693 ((OPJ_UINT64)comp->dx << levelno));
694 try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
695 ((OPJ_UINT64)comp->dy << levelno));
696 trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
697 ((OPJ_UINT64)comp->dx << levelno));
698 try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
699 ((OPJ_UINT64)comp->dy << levelno));
Nicolas Pena088ca032017-09-01 13:25:16 -0400700 rpx = res->pdx + levelno;
701 rpy = res->pdy + levelno;
702
Lei Zhangee35a502024-05-30 17:31:47 +0000703 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
704 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400705 continue;
706 }
707
708 /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
Lei Zhangee35a502024-05-30 17:31:47 +0000709 if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
710 ((pi->y == pi->ty0) &&
711 (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400712 continue;
713 }
Lei Zhangee35a502024-05-30 17:31:47 +0000714 if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
715 ((pi->x == pi->tx0) &&
716 (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
Nicolas Pena088ca032017-09-01 13:25:16 -0400717 continue;
718 }
719
720 if ((res->pw == 0) || (res->ph == 0)) {
721 continue;
722 }
723
724 if ((trx0 == trx1) || (try0 == try1)) {
725 continue;
726 }
727
Lei Zhangee35a502024-05-30 17:31:47 +0000728 prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
729 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000730 - opj_uint_floordivpow2(trx0, res->pdx);
Lei Zhangee35a502024-05-30 17:31:47 +0000731 prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
732 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000733 - opj_uint_floordivpow2(try0, res->pdy);
734 pi->precno = (OPJ_UINT32)(prci + prcj * res->pw);
Nicolas Pena088ca032017-09-01 13:25:16 -0400735 if (pi->precno >= res->pw * res->ph) {
736 return OPJ_FALSE;
737 }
738 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
739 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
740 pi->step_c + pi->precno * pi->step_p;
741 if (index >= pi->include_size) {
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000742 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
Nicolas Pena088ca032017-09-01 13:25:16 -0400743 return OPJ_FALSE;
744 }
745 if (!pi->include[index]) {
746 pi->include[index] = 1;
747 return OPJ_TRUE;
748 }
749LABEL_SKIP:
750 ;
751 }
752 }
753 }
754 }
755 }
756
757 return OPJ_FALSE;
758}
759
760static void opj_get_encoding_parameters(const opj_image_t *p_image,
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700761 const opj_cp_t *p_cp,
Nicolas Pena088ca032017-09-01 13:25:16 -0400762 OPJ_UINT32 p_tileno,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000763 OPJ_UINT32 * p_tx0,
764 OPJ_UINT32 * p_tx1,
765 OPJ_UINT32 * p_ty0,
766 OPJ_UINT32 * p_ty1,
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700767 OPJ_UINT32 * p_dx_min,
768 OPJ_UINT32 * p_dy_min,
769 OPJ_UINT32 * p_max_prec,
Nicolas Pena088ca032017-09-01 13:25:16 -0400770 OPJ_UINT32 * p_max_res)
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700771{
Nicolas Pena088ca032017-09-01 13:25:16 -0400772 /* loop */
773 OPJ_UINT32 compno, resno;
774 /* pointers */
775 const opj_tcp_t *l_tcp = 00;
776 const opj_tccp_t * l_tccp = 00;
777 const opj_image_comp_t * l_img_comp = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700778
Nicolas Pena088ca032017-09-01 13:25:16 -0400779 /* position in x and y of tile */
780 OPJ_UINT32 p, q;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700781
Lei Zhang3dd6ef02019-07-03 18:40:49 +0000782 /* non-corrected (in regard to image offset) tile offset */
783 OPJ_UINT32 l_tx0, l_ty0;
784
Nicolas Pena088ca032017-09-01 13:25:16 -0400785 /* preconditions */
786 assert(p_cp != 00);
787 assert(p_image != 00);
788 assert(p_tileno < p_cp->tw * p_cp->th);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700789
Nicolas Pena088ca032017-09-01 13:25:16 -0400790 /* initializations */
791 l_tcp = &p_cp->tcps [p_tileno];
792 l_img_comp = p_image->comps;
793 l_tccp = l_tcp->tccps;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700794
Nicolas Pena088ca032017-09-01 13:25:16 -0400795 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
796 p = p_tileno % p_cp->tw;
797 q = p_tileno / p_cp->tw;
JUN FANG7dba9862015-02-27 10:00:00 -0800798
Nicolas Pena088ca032017-09-01 13:25:16 -0400799 /* find extent of tile */
Lei Zhang3dd6ef02019-07-03 18:40:49 +0000800 l_tx0 = p_cp->tx0 + p *
801 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000802 *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
803 *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
Lei Zhang3dd6ef02019-07-03 18:40:49 +0000804 l_ty0 = p_cp->ty0 + q *
805 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000806 *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
807 *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700808
Nicolas Pena088ca032017-09-01 13:25:16 -0400809 /* max precision is 0 (can only grow) */
810 *p_max_prec = 0;
811 *p_max_res = 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700812
Nicolas Pena088ca032017-09-01 13:25:16 -0400813 /* take the largest value for dx_min and dy_min */
814 *p_dx_min = 0x7fffffff;
815 *p_dy_min = 0x7fffffff;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700816
Nicolas Pena088ca032017-09-01 13:25:16 -0400817 for (compno = 0; compno < p_image->numcomps; ++compno) {
818 /* arithmetic variables to calculate */
819 OPJ_UINT32 l_level_no;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000820 OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
821 OPJ_UINT32 l_px0, l_py0, l_px1, py1;
Nicolas Pena088ca032017-09-01 13:25:16 -0400822 OPJ_UINT32 l_pdx, l_pdy;
823 OPJ_UINT32 l_pw, l_ph;
824 OPJ_UINT32 l_product;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000825 OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700826
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000827 l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
828 l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
829 l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
830 l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700831
Nicolas Pena088ca032017-09-01 13:25:16 -0400832 if (l_tccp->numresolutions > *p_max_res) {
833 *p_max_res = l_tccp->numresolutions;
834 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700835
Nicolas Pena088ca032017-09-01 13:25:16 -0400836 /* use custom size for precincts */
837 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
Lei Zhangee35a502024-05-30 17:31:47 +0000838 OPJ_UINT64 l_dx, l_dy;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700839
Nicolas Pena088ca032017-09-01 13:25:16 -0400840 /* precinct width and height */
841 l_pdx = l_tccp->prcw[resno];
842 l_pdy = l_tccp->prch[resno];
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700843
Lei Zhangee35a502024-05-30 17:31:47 +0000844 l_dx = l_img_comp->dx * ((OPJ_UINT64)1u << (l_pdx + l_tccp->numresolutions - 1 -
845 resno));
846 l_dy = l_img_comp->dy * ((OPJ_UINT64)1u << (l_pdy + l_tccp->numresolutions - 1 -
847 resno));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700848
Nicolas Pena088ca032017-09-01 13:25:16 -0400849 /* take the minimum size for dx for each comp and resolution */
Lei Zhangee35a502024-05-30 17:31:47 +0000850 if (l_dx <= UINT_MAX) {
851 *p_dx_min = opj_uint_min(*p_dx_min, (OPJ_UINT32)l_dx);
852 }
853 if (l_dy <= UINT_MAX) {
854 *p_dy_min = opj_uint_min(*p_dy_min, (OPJ_UINT32)l_dy);
855 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700856
Nicolas Pena088ca032017-09-01 13:25:16 -0400857 /* various calculations of extents */
858 l_level_no = l_tccp->numresolutions - 1 - resno;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700859
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000860 l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
861 l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
862 l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
863 l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
Nicolas Pena088ca032017-09-01 13:25:16 -0400864
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000865 l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
866 l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
867 l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
Nicolas Pena088ca032017-09-01 13:25:16 -0400868
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000869 py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
Nicolas Pena088ca032017-09-01 13:25:16 -0400870
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000871 l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
872 l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
Nicolas Pena088ca032017-09-01 13:25:16 -0400873
874 l_product = l_pw * l_ph;
875
876 /* update precision */
877 if (l_product > *p_max_prec) {
878 *p_max_prec = l_product;
879 }
880 }
881 ++l_img_comp;
882 ++l_tccp;
883 }
884}
885
886
887static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
888 const opj_cp_t *p_cp,
889 OPJ_UINT32 tileno,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000890 OPJ_UINT32 * p_tx0,
891 OPJ_UINT32 * p_tx1,
892 OPJ_UINT32 * p_ty0,
893 OPJ_UINT32 * p_ty1,
Nicolas Pena088ca032017-09-01 13:25:16 -0400894 OPJ_UINT32 * p_dx_min,
895 OPJ_UINT32 * p_dy_min,
896 OPJ_UINT32 * p_max_prec,
897 OPJ_UINT32 * p_max_res,
898 OPJ_UINT32 ** p_resolutions)
899{
900 /* loop*/
901 OPJ_UINT32 compno, resno;
902
903 /* pointers*/
904 const opj_tcp_t *tcp = 00;
905 const opj_tccp_t * l_tccp = 00;
906 const opj_image_comp_t * l_img_comp = 00;
907
908 /* to store l_dx, l_dy, w and h for each resolution and component.*/
909 OPJ_UINT32 * lResolutionPtr;
910
911 /* position in x and y of tile*/
912 OPJ_UINT32 p, q;
913
914 /* non-corrected (in regard to image offset) tile offset */
915 OPJ_UINT32 l_tx0, l_ty0;
916
917 /* preconditions in debug*/
918 assert(p_cp != 00);
919 assert(p_image != 00);
920 assert(tileno < p_cp->tw * p_cp->th);
921
922 /* initializations*/
923 tcp = &p_cp->tcps [tileno];
924 l_tccp = tcp->tccps;
925 l_img_comp = p_image->comps;
926
927 /* position in x and y of tile*/
928 p = tileno % p_cp->tw;
929 q = tileno / p_cp->tw;
930
931 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
932 l_tx0 = p_cp->tx0 + p *
933 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000934 *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
935 *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
Nicolas Pena088ca032017-09-01 13:25:16 -0400936 l_ty0 = p_cp->ty0 + q *
937 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000938 *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
939 *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
Nicolas Pena088ca032017-09-01 13:25:16 -0400940
941 /* max precision and resolution is 0 (can only grow)*/
942 *p_max_prec = 0;
943 *p_max_res = 0;
944
945 /* take the largest value for dx_min and dy_min*/
946 *p_dx_min = 0x7fffffff;
947 *p_dy_min = 0x7fffffff;
948
949 for (compno = 0; compno < p_image->numcomps; ++compno) {
Lei Zhangc06d7e72022-06-10 22:07:55 +0000950 /* arithmetic variables to calculate*/
Nicolas Pena088ca032017-09-01 13:25:16 -0400951 OPJ_UINT32 l_level_no;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000952 OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
953 OPJ_UINT32 l_px0, l_py0, l_px1, py1;
Nicolas Pena088ca032017-09-01 13:25:16 -0400954 OPJ_UINT32 l_product;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000955 OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
Nicolas Pena088ca032017-09-01 13:25:16 -0400956 OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
957
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000958 lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL;
Nicolas Pena088ca032017-09-01 13:25:16 -0400959
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000960 l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
961 l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
962 l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
963 l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
Nicolas Pena088ca032017-09-01 13:25:16 -0400964
965 if (l_tccp->numresolutions > *p_max_res) {
966 *p_max_res = l_tccp->numresolutions;
967 }
968
969 /* use custom size for precincts*/
970 l_level_no = l_tccp->numresolutions;
971 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
972 OPJ_UINT32 l_dx, l_dy;
973
974 --l_level_no;
975
976 /* precinct width and height*/
977 l_pdx = l_tccp->prcw[resno];
978 l_pdy = l_tccp->prch[resno];
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000979 if (lResolutionPtr) {
980 *lResolutionPtr++ = l_pdx;
981 *lResolutionPtr++ = l_pdy;
982 }
Nicolas Pena088ca032017-09-01 13:25:16 -0400983 if (l_pdx + l_level_no < 32 &&
984 l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
985 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
986 /* take the minimum size for l_dx for each comp and resolution*/
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000987 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
Nicolas Pena088ca032017-09-01 13:25:16 -0400988 }
989 if (l_pdy + l_level_no < 32 &&
990 l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
991 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000992 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
Nicolas Pena088ca032017-09-01 13:25:16 -0400993 }
994
995 /* various calculations of extents*/
Daniel Hosseiniana81ff722021-02-17 20:56:08 +0000996 l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
997 l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
998 l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
999 l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
1000 l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
1001 l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
1002 l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1003 py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1004 l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
1005 l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
1006 if (lResolutionPtr) {
1007 *lResolutionPtr++ = l_pw;
1008 *lResolutionPtr++ = l_ph;
1009 }
Nicolas Pena088ca032017-09-01 13:25:16 -04001010 l_product = l_pw * l_ph;
1011
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001012 /* update precision*/
Nicolas Pena088ca032017-09-01 13:25:16 -04001013 if (l_product > *p_max_prec) {
1014 *p_max_prec = l_product;
1015 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001016
Nicolas Pena088ca032017-09-01 13:25:16 -04001017 }
1018 ++l_tccp;
1019 ++l_img_comp;
1020 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001021}
1022
Nicolas Pena088ca032017-09-01 13:25:16 -04001023static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
1024 const opj_cp_t *cp,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001025 OPJ_UINT32 tileno,
1026 opj_event_mgr_t* manager)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001027{
Nicolas Pena088ca032017-09-01 13:25:16 -04001028 /* loop*/
1029 OPJ_UINT32 pino, compno;
1030 /* number of poc in the p_pi*/
1031 OPJ_UINT32 l_poc_bound;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001032
Nicolas Pena088ca032017-09-01 13:25:16 -04001033 /* pointers to tile coding parameters and components.*/
1034 opj_pi_iterator_t *l_pi = 00;
1035 opj_tcp_t *tcp = 00;
1036 const opj_tccp_t *tccp = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001037
Nicolas Pena088ca032017-09-01 13:25:16 -04001038 /* current packet iterator being allocated*/
1039 opj_pi_iterator_t *l_current_pi = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001040
Nicolas Pena088ca032017-09-01 13:25:16 -04001041 /* preconditions in debug*/
1042 assert(cp != 00);
1043 assert(image != 00);
1044 assert(tileno < cp->tw * cp->th);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001045
Nicolas Pena088ca032017-09-01 13:25:16 -04001046 /* initializations*/
1047 tcp = &cp->tcps[tileno];
1048 l_poc_bound = tcp->numpocs + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001049
Nicolas Pena088ca032017-09-01 13:25:16 -04001050 /* memory allocations*/
1051 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
1052 sizeof(opj_pi_iterator_t));
1053 if (!l_pi) {
1054 return NULL;
1055 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001056
Nicolas Pena088ca032017-09-01 13:25:16 -04001057 l_current_pi = l_pi;
1058 for (pino = 0; pino < l_poc_bound ; ++pino) {
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001059
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001060 l_current_pi->manager = manager;
1061
Nicolas Pena088ca032017-09-01 13:25:16 -04001062 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
1063 sizeof(opj_pi_comp_t));
1064 if (! l_current_pi->comps) {
1065 opj_pi_destroy(l_pi, l_poc_bound);
1066 return NULL;
1067 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001068
Nicolas Pena088ca032017-09-01 13:25:16 -04001069 l_current_pi->numcomps = image->numcomps;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001070
Nicolas Pena088ca032017-09-01 13:25:16 -04001071 for (compno = 0; compno < image->numcomps; ++compno) {
1072 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001073
Nicolas Pena088ca032017-09-01 13:25:16 -04001074 tccp = &tcp->tccps[compno];
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001075
Nicolas Pena088ca032017-09-01 13:25:16 -04001076 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
1077 sizeof(opj_pi_resolution_t));
1078 if (!comp->resolutions) {
1079 opj_pi_destroy(l_pi, l_poc_bound);
1080 return 00;
1081 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001082
Nicolas Pena088ca032017-09-01 13:25:16 -04001083 comp->numresolutions = tccp->numresolutions;
1084 }
1085 ++l_current_pi;
1086 }
1087 return l_pi;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001088}
1089
Nicolas Pena088ca032017-09-01 13:25:16 -04001090static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
1091 OPJ_UINT32 p_tileno,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001092 OPJ_UINT32 p_tx0,
1093 OPJ_UINT32 p_tx1,
1094 OPJ_UINT32 p_ty0,
1095 OPJ_UINT32 p_ty1,
Nicolas Pena088ca032017-09-01 13:25:16 -04001096 OPJ_UINT32 p_max_prec,
1097 OPJ_UINT32 p_max_res,
1098 OPJ_UINT32 p_dx_min,
1099 OPJ_UINT32 p_dy_min)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001100{
Nicolas Pena088ca032017-09-01 13:25:16 -04001101 /* loop*/
1102 OPJ_UINT32 pino;
1103 /* tile coding parameter*/
1104 opj_tcp_t *l_tcp = 00;
1105 /* current poc being updated*/
1106 opj_poc_t * l_current_poc = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001107
Nicolas Pena088ca032017-09-01 13:25:16 -04001108 /* number of pocs*/
1109 OPJ_UINT32 l_poc_bound;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001110
1111 OPJ_ARG_NOT_USED(p_max_res);
1112
Nicolas Pena088ca032017-09-01 13:25:16 -04001113 /* preconditions in debug*/
1114 assert(p_cp != 00);
1115 assert(p_tileno < p_cp->tw * p_cp->th);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001116
Nicolas Pena088ca032017-09-01 13:25:16 -04001117 /* initializations*/
1118 l_tcp = &p_cp->tcps [p_tileno];
1119 /* number of iterations in the loop */
1120 l_poc_bound = l_tcp->numpocs + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001121
Nicolas Pena088ca032017-09-01 13:25:16 -04001122 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1123 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1124 l_current_poc = l_tcp->pocs;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001125
Nicolas Pena088ca032017-09-01 13:25:16 -04001126 l_current_poc->compS = l_current_poc->compno0;
1127 l_current_poc->compE = l_current_poc->compno1;
1128 l_current_poc->resS = l_current_poc->resno0;
1129 l_current_poc->resE = l_current_poc->resno1;
1130 l_current_poc->layE = l_current_poc->layno1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001131
Nicolas Pena088ca032017-09-01 13:25:16 -04001132 /* special treatment for the first element*/
1133 l_current_poc->layS = 0;
1134 l_current_poc->prg = l_current_poc->prg1;
1135 l_current_poc->prcS = 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001136
Nicolas Pena088ca032017-09-01 13:25:16 -04001137 l_current_poc->prcE = p_max_prec;
1138 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1139 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1140 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1141 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1142 l_current_poc->dx = p_dx_min;
1143 l_current_poc->dy = p_dy_min;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001144
Nicolas Pena088ca032017-09-01 13:25:16 -04001145 ++ l_current_poc;
1146 for (pino = 1; pino < l_poc_bound ; ++pino) {
1147 l_current_poc->compS = l_current_poc->compno0;
1148 l_current_poc->compE = l_current_poc->compno1;
1149 l_current_poc->resS = l_current_poc->resno0;
1150 l_current_poc->resE = l_current_poc->resno1;
1151 l_current_poc->layE = l_current_poc->layno1;
1152 l_current_poc->prg = l_current_poc->prg1;
1153 l_current_poc->prcS = 0;
1154 /* special treatment here different from the first element*/
1155 l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
1156 l_current_poc->layE : 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001157
Nicolas Pena088ca032017-09-01 13:25:16 -04001158 l_current_poc->prcE = p_max_prec;
1159 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1160 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1161 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1162 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1163 l_current_poc->dx = p_dx_min;
1164 l_current_poc->dy = p_dy_min;
1165 ++ l_current_poc;
1166 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001167}
1168
Nicolas Pena088ca032017-09-01 13:25:16 -04001169static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
1170 OPJ_UINT32 p_num_comps,
1171 OPJ_UINT32 p_tileno,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001172 OPJ_UINT32 p_tx0,
1173 OPJ_UINT32 p_tx1,
1174 OPJ_UINT32 p_ty0,
1175 OPJ_UINT32 p_ty1,
Nicolas Pena088ca032017-09-01 13:25:16 -04001176 OPJ_UINT32 p_max_prec,
1177 OPJ_UINT32 p_max_res,
1178 OPJ_UINT32 p_dx_min,
1179 OPJ_UINT32 p_dy_min)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001180{
Nicolas Pena088ca032017-09-01 13:25:16 -04001181 /* loop*/
1182 OPJ_UINT32 pino;
1183 /* tile coding parameter*/
1184 opj_tcp_t *l_tcp = 00;
1185 /* current poc being updated*/
1186 opj_poc_t * l_current_poc = 00;
1187 /* number of pocs*/
1188 OPJ_UINT32 l_poc_bound;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001189
Nicolas Pena088ca032017-09-01 13:25:16 -04001190 /* preconditions in debug*/
1191 assert(p_cp != 00);
1192 assert(p_tileno < p_cp->tw * p_cp->th);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001193
Nicolas Pena088ca032017-09-01 13:25:16 -04001194 /* initializations*/
1195 l_tcp = &p_cp->tcps [p_tileno];
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001196
Nicolas Pena088ca032017-09-01 13:25:16 -04001197 /* number of iterations in the loop */
1198 l_poc_bound = l_tcp->numpocs + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001199
Nicolas Pena088ca032017-09-01 13:25:16 -04001200 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1201 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1202 l_current_poc = l_tcp->pocs;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001203
Nicolas Pena088ca032017-09-01 13:25:16 -04001204 for (pino = 0; pino < l_poc_bound ; ++pino) {
1205 l_current_poc->compS = 0;
1206 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1207 l_current_poc->resS = 0;
1208 l_current_poc->resE = p_max_res;
1209 l_current_poc->layS = 0;
1210 l_current_poc->layE = l_tcp->numlayers;
1211 l_current_poc->prg = l_tcp->prg;
1212 l_current_poc->prcS = 0;
1213 l_current_poc->prcE = p_max_prec;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001214 l_current_poc->txS = p_tx0;
1215 l_current_poc->txE = p_tx1;
1216 l_current_poc->tyS = p_ty0;
1217 l_current_poc->tyE = p_ty1;
Nicolas Pena088ca032017-09-01 13:25:16 -04001218 l_current_poc->dx = p_dx_min;
1219 l_current_poc->dy = p_dy_min;
1220 ++ l_current_poc;
1221 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001222}
1223
Nicolas Pena088ca032017-09-01 13:25:16 -04001224static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1225 opj_tcp_t * p_tcp,
1226 OPJ_UINT32 p_max_precision,
1227 OPJ_UINT32 p_max_res)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001228{
Nicolas Pena088ca032017-09-01 13:25:16 -04001229 /* loop*/
1230 OPJ_UINT32 pino;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001231
Lei Zhangc06d7e72022-06-10 22:07:55 +00001232 /* encoding parameters to set*/
Nicolas Pena088ca032017-09-01 13:25:16 -04001233 OPJ_UINT32 l_bound;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001234
Nicolas Pena088ca032017-09-01 13:25:16 -04001235 opj_pi_iterator_t * l_current_pi = 00;
1236 opj_poc_t* l_current_poc = 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001237
1238 OPJ_ARG_NOT_USED(p_max_res);
1239
Nicolas Pena088ca032017-09-01 13:25:16 -04001240 /* preconditions in debug*/
1241 assert(p_pi != 00);
1242 assert(p_tcp != 00);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001243
Nicolas Pena088ca032017-09-01 13:25:16 -04001244 /* initializations*/
1245 l_bound = p_tcp->numpocs + 1;
1246 l_current_pi = p_pi;
1247 l_current_poc = p_tcp->pocs;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001248
Nicolas Pena088ca032017-09-01 13:25:16 -04001249 for (pino = 0; pino < l_bound; ++pino) {
1250 l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1251 l_current_pi->first = 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001252
Nicolas Pena088ca032017-09-01 13:25:16 -04001253 l_current_pi->poc.resno0 =
1254 l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1255 l_current_pi->poc.compno0 =
1256 l_current_poc->compno0; /* Component Index #0 (Start) */
1257 l_current_pi->poc.layno0 = 0;
1258 l_current_pi->poc.precno0 = 0;
1259 l_current_pi->poc.resno1 =
1260 l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1261 l_current_pi->poc.compno1 =
1262 l_current_poc->compno1; /* Component Index #0 (End) */
1263 l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
1264 p_tcp->numlayers); /* Layer Index #0 (End) */
1265 l_current_pi->poc.precno1 = p_max_precision;
1266 ++l_current_pi;
1267 ++l_current_poc;
1268 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001269}
1270
Nicolas Pena088ca032017-09-01 13:25:16 -04001271static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1272 opj_tcp_t * p_tcp,
1273 OPJ_UINT32 p_max_precision,
1274 OPJ_UINT32 p_max_res)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001275{
Nicolas Pena088ca032017-09-01 13:25:16 -04001276 /* loop*/
1277 OPJ_UINT32 pino;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001278
Lei Zhangc06d7e72022-06-10 22:07:55 +00001279 /* encoding parameters to set*/
Nicolas Pena088ca032017-09-01 13:25:16 -04001280 OPJ_UINT32 l_bound;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001281
Nicolas Pena088ca032017-09-01 13:25:16 -04001282 opj_pi_iterator_t * l_current_pi = 00;
1283 /* preconditions in debug*/
1284 assert(p_tcp != 00);
1285 assert(p_pi != 00);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001286
Nicolas Pena088ca032017-09-01 13:25:16 -04001287 /* initializations*/
1288 l_bound = p_tcp->numpocs + 1;
1289 l_current_pi = p_pi;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001290
Nicolas Pena088ca032017-09-01 13:25:16 -04001291 for (pino = 0; pino < l_bound; ++pino) {
1292 l_current_pi->poc.prg = p_tcp->prg;
1293 l_current_pi->first = 1;
1294 l_current_pi->poc.resno0 = 0;
1295 l_current_pi->poc.compno0 = 0;
1296 l_current_pi->poc.layno0 = 0;
1297 l_current_pi->poc.precno0 = 0;
1298 l_current_pi->poc.resno1 = p_max_res;
1299 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1300 l_current_pi->poc.layno1 = p_tcp->numlayers;
1301 l_current_pi->poc.precno1 = p_max_precision;
1302 ++l_current_pi;
1303 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001304}
1305
1306
1307
Nicolas Pena088ca032017-09-01 13:25:16 -04001308static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1309 opj_cp_t *cp,
1310 OPJ_UINT32 tileno,
1311 OPJ_UINT32 pino,
1312 const OPJ_CHAR *prog)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001313{
Nicolas Pena088ca032017-09-01 13:25:16 -04001314 OPJ_INT32 i;
1315 opj_tcp_t *tcps = &cp->tcps[tileno];
1316 opj_poc_t *tcp = &tcps->pocs[pino];
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001317
Nicolas Pena088ca032017-09-01 13:25:16 -04001318 if (pos >= 0) {
Lei Zhangc06d7e72022-06-10 22:07:55 +00001319 for (i = pos; i >= 0; i--) {
Nicolas Pena088ca032017-09-01 13:25:16 -04001320 switch (prog[i]) {
1321 case 'R':
1322 if (tcp->res_t == tcp->resE) {
1323 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1324 return OPJ_TRUE;
1325 } else {
1326 return OPJ_FALSE;
1327 }
1328 } else {
1329 return OPJ_TRUE;
1330 }
1331 break;
1332 case 'C':
1333 if (tcp->comp_t == tcp->compE) {
1334 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1335 return OPJ_TRUE;
1336 } else {
1337 return OPJ_FALSE;
1338 }
1339 } else {
1340 return OPJ_TRUE;
1341 }
1342 break;
1343 case 'L':
1344 if (tcp->lay_t == tcp->layE) {
1345 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1346 return OPJ_TRUE;
1347 } else {
1348 return OPJ_FALSE;
1349 }
1350 } else {
1351 return OPJ_TRUE;
1352 }
1353 break;
1354 case 'P':
1355 switch (tcp->prg) {
1356 case OPJ_LRCP: /* fall through */
1357 case OPJ_RLCP:
1358 if (tcp->prc_t == tcp->prcE) {
1359 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1360 return OPJ_TRUE;
1361 } else {
1362 return OPJ_FALSE;
1363 }
1364 } else {
1365 return OPJ_TRUE;
1366 }
1367 break;
1368 default:
1369 if (tcp->tx0_t == tcp->txE) {
1370 /*TY*/
1371 if (tcp->ty0_t == tcp->tyE) {
1372 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1373 return OPJ_TRUE;
1374 } else {
1375 return OPJ_FALSE;
1376 }
1377 } else {
1378 return OPJ_TRUE;
1379 }/*TY*/
1380 } else {
1381 return OPJ_TRUE;
1382 }
1383 break;
1384 }/*end case P*/
1385 }/*end switch*/
1386 }/*end for*/
1387 }/*end if*/
1388 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001389}
1390
1391
1392/*
1393==========================================================
1394 Packet iterator interface
1395==========================================================
1396*/
1397opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
Nicolas Pena088ca032017-09-01 13:25:16 -04001398 opj_cp_t *p_cp,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001399 OPJ_UINT32 p_tile_no,
1400 opj_event_mgr_t* manager)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001401{
Nicolas Pena088ca032017-09-01 13:25:16 -04001402 OPJ_UINT32 numcomps = p_image->numcomps;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001403
Nicolas Pena088ca032017-09-01 13:25:16 -04001404 /* loop */
1405 OPJ_UINT32 pino;
1406 OPJ_UINT32 compno, resno;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001407
Lei Zhangc06d7e72022-06-10 22:07:55 +00001408 /* to store w, h, dx and dy for all components and resolutions */
Nicolas Pena088ca032017-09-01 13:25:16 -04001409 OPJ_UINT32 * l_tmp_data;
1410 OPJ_UINT32 ** l_tmp_ptr;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001411
Lei Zhangc06d7e72022-06-10 22:07:55 +00001412 /* encoding parameters to set */
Nicolas Pena088ca032017-09-01 13:25:16 -04001413 OPJ_UINT32 l_max_res;
1414 OPJ_UINT32 l_max_prec;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001415 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
Nicolas Pena088ca032017-09-01 13:25:16 -04001416 OPJ_UINT32 l_dx_min, l_dy_min;
1417 OPJ_UINT32 l_bound;
1418 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1419 OPJ_UINT32 l_data_stride;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001420
Nicolas Pena088ca032017-09-01 13:25:16 -04001421 /* pointers */
1422 opj_pi_iterator_t *l_pi = 00;
1423 opj_tcp_t *l_tcp = 00;
1424 const opj_tccp_t *l_tccp = 00;
1425 opj_pi_comp_t *l_current_comp = 00;
1426 opj_image_comp_t * l_img_comp = 00;
1427 opj_pi_iterator_t * l_current_pi = 00;
1428 OPJ_UINT32 * l_encoding_value_ptr = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001429
Nicolas Pena088ca032017-09-01 13:25:16 -04001430 /* preconditions in debug */
1431 assert(p_cp != 00);
1432 assert(p_image != 00);
1433 assert(p_tile_no < p_cp->tw * p_cp->th);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001434
Nicolas Pena088ca032017-09-01 13:25:16 -04001435 /* initializations */
1436 l_tcp = &p_cp->tcps[p_tile_no];
1437 l_bound = l_tcp->numpocs + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001438
Nicolas Pena088ca032017-09-01 13:25:16 -04001439 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1440 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1441 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1442 if
1443 (! l_tmp_data) {
1444 return 00;
1445 }
1446 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1447 numcomps * sizeof(OPJ_UINT32 *));
1448 if
1449 (! l_tmp_ptr) {
1450 opj_free(l_tmp_data);
1451 return 00;
1452 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001453
Nicolas Pena088ca032017-09-01 13:25:16 -04001454 /* memory allocation for pi */
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001455 l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
Nicolas Pena088ca032017-09-01 13:25:16 -04001456 if (!l_pi) {
1457 opj_free(l_tmp_data);
1458 opj_free(l_tmp_ptr);
1459 return 00;
1460 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001461
Nicolas Pena088ca032017-09-01 13:25:16 -04001462 l_encoding_value_ptr = l_tmp_data;
1463 /* update pointer array */
1464 for
1465 (compno = 0; compno < numcomps; ++compno) {
1466 l_tmp_ptr[compno] = l_encoding_value_ptr;
1467 l_encoding_value_ptr += l_data_stride;
1468 }
1469 /* get encoding parameters */
1470 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1471 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001472
Nicolas Pena088ca032017-09-01 13:25:16 -04001473 /* step calculations */
1474 l_step_p = 1;
1475 l_step_c = l_max_prec * l_step_p;
1476 l_step_r = numcomps * l_step_c;
1477 l_step_l = l_max_res * l_step_r;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001478
Nicolas Pena088ca032017-09-01 13:25:16 -04001479 /* set values for first packet iterator */
1480 l_current_pi = l_pi;
JUN FANG83ad95c2015-07-13 06:34:20 -07001481
Nicolas Pena088ca032017-09-01 13:25:16 -04001482 /* memory allocation for include */
1483 /* prevent an integer overflow issue */
1484 /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1485 l_current_pi->include = 00;
1486 if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
1487 l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
1488 l_current_pi->include = (OPJ_INT16*) opj_calloc(
1489 l_current_pi->include_size, sizeof(OPJ_INT16));
1490 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001491
Nicolas Pena088ca032017-09-01 13:25:16 -04001492 if (!l_current_pi->include) {
1493 opj_free(l_tmp_data);
1494 opj_free(l_tmp_ptr);
1495 opj_pi_destroy(l_pi, l_bound);
1496 return 00;
1497 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001498
Nicolas Pena088ca032017-09-01 13:25:16 -04001499 /* special treatment for the first packet iterator */
1500 l_current_comp = l_current_pi->comps;
1501 l_img_comp = p_image->comps;
1502 l_tccp = l_tcp->tccps;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001503
Nicolas Pena088ca032017-09-01 13:25:16 -04001504 l_current_pi->tx0 = l_tx0;
1505 l_current_pi->ty0 = l_ty0;
1506 l_current_pi->tx1 = l_tx1;
1507 l_current_pi->ty1 = l_ty1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001508
Nicolas Pena088ca032017-09-01 13:25:16 -04001509 /*l_current_pi->dx = l_img_comp->dx;*/
1510 /*l_current_pi->dy = l_img_comp->dy;*/
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001511
Nicolas Pena088ca032017-09-01 13:25:16 -04001512 l_current_pi->step_p = l_step_p;
1513 l_current_pi->step_c = l_step_c;
1514 l_current_pi->step_r = l_step_r;
1515 l_current_pi->step_l = l_step_l;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001516
Nicolas Pena088ca032017-09-01 13:25:16 -04001517 /* allocation for components and number of components has already been calculated by opj_pi_create */
1518 for
1519 (compno = 0; compno < numcomps; ++compno) {
1520 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1521 l_encoding_value_ptr = l_tmp_ptr[compno];
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001522
Nicolas Pena088ca032017-09-01 13:25:16 -04001523 l_current_comp->dx = l_img_comp->dx;
1524 l_current_comp->dy = l_img_comp->dy;
1525 /* resolutions have already been initialized */
1526 for
1527 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1528 l_res->pdx = *(l_encoding_value_ptr++);
1529 l_res->pdy = *(l_encoding_value_ptr++);
1530 l_res->pw = *(l_encoding_value_ptr++);
1531 l_res->ph = *(l_encoding_value_ptr++);
1532 ++l_res;
1533 }
1534 ++l_current_comp;
1535 ++l_img_comp;
1536 ++l_tccp;
1537 }
1538 ++l_current_pi;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001539
Nicolas Pena088ca032017-09-01 13:25:16 -04001540 for (pino = 1 ; pino < l_bound ; ++pino) {
1541 l_current_comp = l_current_pi->comps;
1542 l_img_comp = p_image->comps;
1543 l_tccp = l_tcp->tccps;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001544
Nicolas Pena088ca032017-09-01 13:25:16 -04001545 l_current_pi->tx0 = l_tx0;
1546 l_current_pi->ty0 = l_ty0;
1547 l_current_pi->tx1 = l_tx1;
1548 l_current_pi->ty1 = l_ty1;
1549 /*l_current_pi->dx = l_dx_min;*/
1550 /*l_current_pi->dy = l_dy_min;*/
1551 l_current_pi->step_p = l_step_p;
1552 l_current_pi->step_c = l_step_c;
1553 l_current_pi->step_r = l_step_r;
1554 l_current_pi->step_l = l_step_l;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001555
Nicolas Pena088ca032017-09-01 13:25:16 -04001556 /* allocation for components and number of components has already been calculated by opj_pi_create */
1557 for
1558 (compno = 0; compno < numcomps; ++compno) {
1559 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1560 l_encoding_value_ptr = l_tmp_ptr[compno];
1561
1562 l_current_comp->dx = l_img_comp->dx;
1563 l_current_comp->dy = l_img_comp->dy;
1564 /* resolutions have already been initialized */
1565 for
1566 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1567 l_res->pdx = *(l_encoding_value_ptr++);
1568 l_res->pdy = *(l_encoding_value_ptr++);
1569 l_res->pw = *(l_encoding_value_ptr++);
1570 l_res->ph = *(l_encoding_value_ptr++);
1571 ++l_res;
1572 }
1573 ++l_current_comp;
1574 ++l_img_comp;
1575 ++l_tccp;
1576 }
1577 /* special treatment*/
1578 l_current_pi->include = (l_current_pi - 1)->include;
1579 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1580 ++l_current_pi;
1581 }
1582 opj_free(l_tmp_data);
1583 l_tmp_data = 00;
1584 opj_free(l_tmp_ptr);
1585 l_tmp_ptr = 00;
1586 if
1587 (l_tcp->POC) {
1588 opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1589 } else {
1590 opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1591 }
1592 return l_pi;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001593}
1594
1595
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001596OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image,
1597 const opj_cp_t *p_cp,
1598 OPJ_UINT32 p_tile_no)
1599{
1600 OPJ_UINT32 l_max_res;
1601 OPJ_UINT32 l_max_prec;
1602 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1603 OPJ_UINT32 l_dx_min, l_dy_min;
1604
1605 /* preconditions in debug*/
1606 assert(p_cp != 00);
1607 assert(p_image != 00);
1608 assert(p_tile_no < p_cp->tw * p_cp->th);
1609
1610 /* get encoding parameters*/
1611 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1612 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL);
1613
1614 return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps *
1615 l_max_res;
1616}
1617
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001618
1619opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
Nicolas Pena088ca032017-09-01 13:25:16 -04001620 opj_cp_t *p_cp,
1621 OPJ_UINT32 p_tile_no,
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001622 J2K_T2_MODE p_t2_mode,
1623 opj_event_mgr_t* manager)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001624{
Nicolas Pena088ca032017-09-01 13:25:16 -04001625 OPJ_UINT32 numcomps = p_image->numcomps;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001626
Nicolas Pena088ca032017-09-01 13:25:16 -04001627 /* loop*/
1628 OPJ_UINT32 pino;
1629 OPJ_UINT32 compno, resno;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001630
Lei Zhangc06d7e72022-06-10 22:07:55 +00001631 /* to store w, h, dx and dy for all components and resolutions*/
Nicolas Pena088ca032017-09-01 13:25:16 -04001632 OPJ_UINT32 * l_tmp_data;
1633 OPJ_UINT32 ** l_tmp_ptr;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001634
Lei Zhangc06d7e72022-06-10 22:07:55 +00001635 /* encoding parameters to set*/
Nicolas Pena088ca032017-09-01 13:25:16 -04001636 OPJ_UINT32 l_max_res;
1637 OPJ_UINT32 l_max_prec;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001638 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
Nicolas Pena088ca032017-09-01 13:25:16 -04001639 OPJ_UINT32 l_dx_min, l_dy_min;
1640 OPJ_UINT32 l_bound;
1641 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1642 OPJ_UINT32 l_data_stride;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001643
Nicolas Pena088ca032017-09-01 13:25:16 -04001644 /* pointers*/
1645 opj_pi_iterator_t *l_pi = 00;
1646 opj_tcp_t *l_tcp = 00;
1647 const opj_tccp_t *l_tccp = 00;
1648 opj_pi_comp_t *l_current_comp = 00;
1649 opj_image_comp_t * l_img_comp = 00;
1650 opj_pi_iterator_t * l_current_pi = 00;
1651 OPJ_UINT32 * l_encoding_value_ptr = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001652
Nicolas Pena088ca032017-09-01 13:25:16 -04001653 /* preconditions in debug*/
1654 assert(p_cp != 00);
1655 assert(p_image != 00);
1656 assert(p_tile_no < p_cp->tw * p_cp->th);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001657
Nicolas Pena088ca032017-09-01 13:25:16 -04001658 /* initializations*/
1659 l_tcp = &p_cp->tcps[p_tile_no];
1660 l_bound = l_tcp->numpocs + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001661
Nicolas Pena088ca032017-09-01 13:25:16 -04001662 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1663 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1664 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1665 if (! l_tmp_data) {
1666 return 00;
1667 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001668
Nicolas Pena088ca032017-09-01 13:25:16 -04001669 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1670 numcomps * sizeof(OPJ_UINT32 *));
1671 if (! l_tmp_ptr) {
1672 opj_free(l_tmp_data);
1673 return 00;
1674 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001675
Nicolas Pena088ca032017-09-01 13:25:16 -04001676 /* memory allocation for pi*/
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001677 l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
Nicolas Pena088ca032017-09-01 13:25:16 -04001678 if (!l_pi) {
1679 opj_free(l_tmp_data);
1680 opj_free(l_tmp_ptr);
1681 return 00;
1682 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001683
Nicolas Pena088ca032017-09-01 13:25:16 -04001684 l_encoding_value_ptr = l_tmp_data;
1685 /* update pointer array*/
1686 for (compno = 0; compno < numcomps; ++compno) {
1687 l_tmp_ptr[compno] = l_encoding_value_ptr;
1688 l_encoding_value_ptr += l_data_stride;
1689 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001690
Nicolas Pena088ca032017-09-01 13:25:16 -04001691 /* get encoding parameters*/
1692 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1693 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001694
Nicolas Pena088ca032017-09-01 13:25:16 -04001695 /* step calculations*/
1696 l_step_p = 1;
1697 l_step_c = l_max_prec * l_step_p;
1698 l_step_r = numcomps * l_step_c;
1699 l_step_l = l_max_res * l_step_r;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001700
Nicolas Pena088ca032017-09-01 13:25:16 -04001701 /* set values for first packet iterator*/
1702 l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1703 l_current_pi = l_pi;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001704
Nicolas Pena088ca032017-09-01 13:25:16 -04001705 /* memory allocation for include*/
1706 l_current_pi->include_size = l_tcp->numlayers * l_step_l;
1707 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
1708 sizeof(OPJ_INT16));
1709 if (!l_current_pi->include) {
1710 opj_free(l_tmp_data);
1711 opj_free(l_tmp_ptr);
1712 opj_pi_destroy(l_pi, l_bound);
1713 return 00;
1714 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001715
Nicolas Pena088ca032017-09-01 13:25:16 -04001716 /* special treatment for the first packet iterator*/
1717 l_current_comp = l_current_pi->comps;
1718 l_img_comp = p_image->comps;
1719 l_tccp = l_tcp->tccps;
1720 l_current_pi->tx0 = l_tx0;
1721 l_current_pi->ty0 = l_ty0;
1722 l_current_pi->tx1 = l_tx1;
1723 l_current_pi->ty1 = l_ty1;
1724 l_current_pi->dx = l_dx_min;
1725 l_current_pi->dy = l_dy_min;
1726 l_current_pi->step_p = l_step_p;
1727 l_current_pi->step_c = l_step_c;
1728 l_current_pi->step_r = l_step_r;
1729 l_current_pi->step_l = l_step_l;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001730
Nicolas Pena088ca032017-09-01 13:25:16 -04001731 /* allocation for components and number of components has already been calculated by opj_pi_create */
1732 for (compno = 0; compno < numcomps; ++compno) {
1733 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1734 l_encoding_value_ptr = l_tmp_ptr[compno];
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001735
Nicolas Pena088ca032017-09-01 13:25:16 -04001736 l_current_comp->dx = l_img_comp->dx;
1737 l_current_comp->dy = l_img_comp->dy;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001738
Nicolas Pena088ca032017-09-01 13:25:16 -04001739 /* resolutions have already been initialized */
1740 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1741 l_res->pdx = *(l_encoding_value_ptr++);
1742 l_res->pdy = *(l_encoding_value_ptr++);
1743 l_res->pw = *(l_encoding_value_ptr++);
1744 l_res->ph = *(l_encoding_value_ptr++);
1745 ++l_res;
1746 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001747
Nicolas Pena088ca032017-09-01 13:25:16 -04001748 ++l_current_comp;
1749 ++l_img_comp;
1750 ++l_tccp;
1751 }
1752 ++l_current_pi;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001753
Nicolas Pena088ca032017-09-01 13:25:16 -04001754 for (pino = 1 ; pino < l_bound ; ++pino) {
1755 l_current_comp = l_current_pi->comps;
1756 l_img_comp = p_image->comps;
1757 l_tccp = l_tcp->tccps;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001758
Nicolas Pena088ca032017-09-01 13:25:16 -04001759 l_current_pi->tx0 = l_tx0;
1760 l_current_pi->ty0 = l_ty0;
1761 l_current_pi->tx1 = l_tx1;
1762 l_current_pi->ty1 = l_ty1;
1763 l_current_pi->dx = l_dx_min;
1764 l_current_pi->dy = l_dy_min;
1765 l_current_pi->step_p = l_step_p;
1766 l_current_pi->step_c = l_step_c;
1767 l_current_pi->step_r = l_step_r;
1768 l_current_pi->step_l = l_step_l;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001769
Nicolas Pena088ca032017-09-01 13:25:16 -04001770 /* allocation for components and number of components has already been calculated by opj_pi_create */
1771 for (compno = 0; compno < numcomps; ++compno) {
1772 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1773 l_encoding_value_ptr = l_tmp_ptr[compno];
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001774
Nicolas Pena088ca032017-09-01 13:25:16 -04001775 l_current_comp->dx = l_img_comp->dx;
1776 l_current_comp->dy = l_img_comp->dy;
1777 /* resolutions have already been initialized */
1778 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1779 l_res->pdx = *(l_encoding_value_ptr++);
1780 l_res->pdy = *(l_encoding_value_ptr++);
1781 l_res->pw = *(l_encoding_value_ptr++);
1782 l_res->ph = *(l_encoding_value_ptr++);
1783 ++l_res;
1784 }
1785 ++l_current_comp;
1786 ++l_img_comp;
1787 ++l_tccp;
1788 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001789
Nicolas Pena088ca032017-09-01 13:25:16 -04001790 /* special treatment*/
1791 l_current_pi->include = (l_current_pi - 1)->include;
1792 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1793 ++l_current_pi;
1794 }
1795
1796 opj_free(l_tmp_data);
1797 l_tmp_data = 00;
1798 opj_free(l_tmp_ptr);
1799 l_tmp_ptr = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001800
Bo Xud53e6fd2014-09-30 11:12:05 -07001801 if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
Nicolas Pena088ca032017-09-01 13:25:16 -04001802 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1803 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1804 } else {
1805 opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
1806 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1807 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001808
Nicolas Pena088ca032017-09-01 13:25:16 -04001809 return l_pi;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001810}
1811
Nicolas Pena088ca032017-09-01 13:25:16 -04001812void opj_pi_create_encode(opj_pi_iterator_t *pi,
1813 opj_cp_t *cp,
1814 OPJ_UINT32 tileno,
1815 OPJ_UINT32 pino,
1816 OPJ_UINT32 tpnum,
1817 OPJ_INT32 tppos,
1818 J2K_T2_MODE t2_mode)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001819{
Nicolas Pena088ca032017-09-01 13:25:16 -04001820 const OPJ_CHAR *prog;
1821 OPJ_INT32 i;
1822 OPJ_UINT32 incr_top = 1, resetX = 0;
1823 opj_tcp_t *tcps = &cp->tcps[tileno];
1824 opj_poc_t *tcp = &tcps->pocs[pino];
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001825
Nicolas Pena088ca032017-09-01 13:25:16 -04001826 prog = opj_j2k_convert_progression_order(tcp->prg);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001827
Nicolas Pena088ca032017-09-01 13:25:16 -04001828 pi[pino].first = 1;
1829 pi[pino].poc.prg = tcp->prg;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001830
Nicolas Pena088ca032017-09-01 13:25:16 -04001831 if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001832 !OPJ_IS_IMF(cp->rsiz) &&
1833 (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) {
Nicolas Pena088ca032017-09-01 13:25:16 -04001834 pi[pino].poc.resno0 = tcp->resS;
1835 pi[pino].poc.resno1 = tcp->resE;
1836 pi[pino].poc.compno0 = tcp->compS;
1837 pi[pino].poc.compno1 = tcp->compE;
1838 pi[pino].poc.layno0 = tcp->layS;
1839 pi[pino].poc.layno1 = tcp->layE;
1840 pi[pino].poc.precno0 = tcp->prcS;
1841 pi[pino].poc.precno1 = tcp->prcE;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001842 pi[pino].poc.tx0 = tcp->txS;
1843 pi[pino].poc.ty0 = tcp->tyS;
1844 pi[pino].poc.tx1 = tcp->txE;
1845 pi[pino].poc.ty1 = tcp->tyE;
Nicolas Pena088ca032017-09-01 13:25:16 -04001846 } else {
1847 for (i = tppos + 1; i < 4; i++) {
1848 switch (prog[i]) {
1849 case 'R':
1850 pi[pino].poc.resno0 = tcp->resS;
1851 pi[pino].poc.resno1 = tcp->resE;
1852 break;
1853 case 'C':
1854 pi[pino].poc.compno0 = tcp->compS;
1855 pi[pino].poc.compno1 = tcp->compE;
1856 break;
1857 case 'L':
1858 pi[pino].poc.layno0 = tcp->layS;
1859 pi[pino].poc.layno1 = tcp->layE;
1860 break;
1861 case 'P':
1862 switch (tcp->prg) {
1863 case OPJ_LRCP:
1864 case OPJ_RLCP:
1865 pi[pino].poc.precno0 = tcp->prcS;
1866 pi[pino].poc.precno1 = tcp->prcE;
1867 break;
1868 default:
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001869 pi[pino].poc.tx0 = tcp->txS;
1870 pi[pino].poc.ty0 = tcp->tyS;
1871 pi[pino].poc.tx1 = tcp->txE;
1872 pi[pino].poc.ty1 = tcp->tyE;
Nicolas Pena088ca032017-09-01 13:25:16 -04001873 break;
1874 }
1875 break;
1876 }
1877 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001878
Nicolas Pena088ca032017-09-01 13:25:16 -04001879 if (tpnum == 0) {
1880 for (i = tppos; i >= 0; i--) {
1881 switch (prog[i]) {
1882 case 'C':
1883 tcp->comp_t = tcp->compS;
1884 pi[pino].poc.compno0 = tcp->comp_t;
1885 pi[pino].poc.compno1 = tcp->comp_t + 1;
1886 tcp->comp_t += 1;
1887 break;
1888 case 'R':
1889 tcp->res_t = tcp->resS;
1890 pi[pino].poc.resno0 = tcp->res_t;
1891 pi[pino].poc.resno1 = tcp->res_t + 1;
1892 tcp->res_t += 1;
1893 break;
1894 case 'L':
1895 tcp->lay_t = tcp->layS;
1896 pi[pino].poc.layno0 = tcp->lay_t;
1897 pi[pino].poc.layno1 = tcp->lay_t + 1;
1898 tcp->lay_t += 1;
1899 break;
1900 case 'P':
1901 switch (tcp->prg) {
1902 case OPJ_LRCP:
1903 case OPJ_RLCP:
1904 tcp->prc_t = tcp->prcS;
1905 pi[pino].poc.precno0 = tcp->prc_t;
1906 pi[pino].poc.precno1 = tcp->prc_t + 1;
1907 tcp->prc_t += 1;
1908 break;
1909 default:
1910 tcp->tx0_t = tcp->txS;
1911 tcp->ty0_t = tcp->tyS;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001912 pi[pino].poc.tx0 = tcp->tx0_t;
1913 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1914 pi[pino].poc.ty0 = tcp->ty0_t;
1915 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
Nicolas Pena088ca032017-09-01 13:25:16 -04001916 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1917 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1918 break;
1919 }
1920 break;
1921 }
1922 }
1923 incr_top = 1;
1924 } else {
1925 for (i = tppos; i >= 0; i--) {
1926 switch (prog[i]) {
1927 case 'C':
1928 pi[pino].poc.compno0 = tcp->comp_t - 1;
1929 pi[pino].poc.compno1 = tcp->comp_t;
1930 break;
1931 case 'R':
1932 pi[pino].poc.resno0 = tcp->res_t - 1;
1933 pi[pino].poc.resno1 = tcp->res_t;
1934 break;
1935 case 'L':
1936 pi[pino].poc.layno0 = tcp->lay_t - 1;
1937 pi[pino].poc.layno1 = tcp->lay_t;
1938 break;
1939 case 'P':
1940 switch (tcp->prg) {
1941 case OPJ_LRCP:
1942 case OPJ_RLCP:
1943 pi[pino].poc.precno0 = tcp->prc_t - 1;
1944 pi[pino].poc.precno1 = tcp->prc_t;
1945 break;
1946 default:
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00001947 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1948 pi[pino].poc.tx1 = tcp->tx0_t ;
1949 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1950 pi[pino].poc.ty1 = tcp->ty0_t ;
Nicolas Pena088ca032017-09-01 13:25:16 -04001951 break;
1952 }
1953 break;
1954 }
1955 if (incr_top == 1) {
1956 switch (prog[i]) {
1957 case 'R':
1958 if (tcp->res_t == tcp->resE) {
1959 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1960 tcp->res_t = tcp->resS;
1961 pi[pino].poc.resno0 = tcp->res_t;
1962 pi[pino].poc.resno1 = tcp->res_t + 1;
1963 tcp->res_t += 1;
1964 incr_top = 1;
1965 } else {
1966 incr_top = 0;
1967 }
1968 } else {
1969 pi[pino].poc.resno0 = tcp->res_t;
1970 pi[pino].poc.resno1 = tcp->res_t + 1;
1971 tcp->res_t += 1;
1972 incr_top = 0;
1973 }
1974 break;
1975 case 'C':
1976 if (tcp->comp_t == tcp->compE) {
1977 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1978 tcp->comp_t = tcp->compS;
1979 pi[pino].poc.compno0 = tcp->comp_t;
1980 pi[pino].poc.compno1 = tcp->comp_t + 1;
1981 tcp->comp_t += 1;
1982 incr_top = 1;
1983 } else {
1984 incr_top = 0;
1985 }
1986 } else {
1987 pi[pino].poc.compno0 = tcp->comp_t;
1988 pi[pino].poc.compno1 = tcp->comp_t + 1;
1989 tcp->comp_t += 1;
1990 incr_top = 0;
1991 }
1992 break;
1993 case 'L':
1994 if (tcp->lay_t == tcp->layE) {
1995 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1996 tcp->lay_t = tcp->layS;
1997 pi[pino].poc.layno0 = tcp->lay_t;
1998 pi[pino].poc.layno1 = tcp->lay_t + 1;
1999 tcp->lay_t += 1;
2000 incr_top = 1;
2001 } else {
2002 incr_top = 0;
2003 }
2004 } else {
2005 pi[pino].poc.layno0 = tcp->lay_t;
2006 pi[pino].poc.layno1 = tcp->lay_t + 1;
2007 tcp->lay_t += 1;
2008 incr_top = 0;
2009 }
2010 break;
2011 case 'P':
2012 switch (tcp->prg) {
2013 case OPJ_LRCP:
2014 case OPJ_RLCP:
2015 if (tcp->prc_t == tcp->prcE) {
2016 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2017 tcp->prc_t = tcp->prcS;
2018 pi[pino].poc.precno0 = tcp->prc_t;
2019 pi[pino].poc.precno1 = tcp->prc_t + 1;
2020 tcp->prc_t += 1;
2021 incr_top = 1;
2022 } else {
2023 incr_top = 0;
2024 }
2025 } else {
2026 pi[pino].poc.precno0 = tcp->prc_t;
2027 pi[pino].poc.precno1 = tcp->prc_t + 1;
2028 tcp->prc_t += 1;
2029 incr_top = 0;
2030 }
2031 break;
2032 default:
2033 if (tcp->tx0_t >= tcp->txE) {
2034 if (tcp->ty0_t >= tcp->tyE) {
2035 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2036 tcp->ty0_t = tcp->tyS;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00002037 pi[pino].poc.ty0 = tcp->ty0_t;
2038 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
Nicolas Pena088ca032017-09-01 13:25:16 -04002039 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2040 incr_top = 1;
2041 resetX = 1;
2042 } else {
2043 incr_top = 0;
2044 resetX = 0;
2045 }
2046 } else {
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00002047 pi[pino].poc.ty0 = tcp->ty0_t;
2048 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
Nicolas Pena088ca032017-09-01 13:25:16 -04002049 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2050 incr_top = 0;
2051 resetX = 1;
2052 }
2053 if (resetX == 1) {
2054 tcp->tx0_t = tcp->txS;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00002055 pi[pino].poc.tx0 = tcp->tx0_t;
2056 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
Nicolas Pena088ca032017-09-01 13:25:16 -04002057 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2058 }
2059 } else {
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00002060 pi[pino].poc.tx0 = tcp->tx0_t;
2061 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
Nicolas Pena088ca032017-09-01 13:25:16 -04002062 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2063 incr_top = 0;
2064 }
2065 break;
2066 }
2067 break;
2068 }
2069 }
2070 }
2071 }
2072 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002073}
2074
2075void opj_pi_destroy(opj_pi_iterator_t *p_pi,
2076 OPJ_UINT32 p_nb_elements)
2077{
Nicolas Pena088ca032017-09-01 13:25:16 -04002078 OPJ_UINT32 compno, pino;
2079 opj_pi_iterator_t *l_current_pi = p_pi;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002080 if (p_pi) {
Nicolas Pena088ca032017-09-01 13:25:16 -04002081 if (p_pi->include) {
2082 opj_free(p_pi->include);
2083 p_pi->include = 00;
2084 }
2085 for (pino = 0; pino < p_nb_elements; ++pino) {
2086 if (l_current_pi->comps) {
2087 opj_pi_comp_t *l_current_component = l_current_pi->comps;
2088 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
2089 if (l_current_component->resolutions) {
2090 opj_free(l_current_component->resolutions);
2091 l_current_component->resolutions = 00;
2092 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002093
Nicolas Pena088ca032017-09-01 13:25:16 -04002094 ++l_current_component;
2095 }
2096 opj_free(l_current_pi->comps);
2097 l_current_pi->comps = 0;
2098 }
2099 ++l_current_pi;
2100 }
2101 opj_free(p_pi);
2102 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002103}
2104
2105
2106
Nicolas Pena088ca032017-09-01 13:25:16 -04002107void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
2108 opj_cp_t *p_cp,
2109 OPJ_UINT32 p_tile_no)
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002110{
Nicolas Pena088ca032017-09-01 13:25:16 -04002111 /* encoding parameters to set */
2112 OPJ_UINT32 l_max_res;
2113 OPJ_UINT32 l_max_prec;
Daniel Hosseiniana81ff722021-02-17 20:56:08 +00002114 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
Nicolas Pena088ca032017-09-01 13:25:16 -04002115 OPJ_UINT32 l_dx_min, l_dy_min;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002116
Nicolas Pena088ca032017-09-01 13:25:16 -04002117 /* pointers */
2118 opj_tcp_t *l_tcp = 00;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002119
Nicolas Pena088ca032017-09-01 13:25:16 -04002120 /* preconditions */
2121 assert(p_cp != 00);
2122 assert(p_image != 00);
2123 assert(p_tile_no < p_cp->tw * p_cp->th);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002124
Nicolas Pena088ca032017-09-01 13:25:16 -04002125 l_tcp = &(p_cp->tcps[p_tile_no]);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002126
Nicolas Pena088ca032017-09-01 13:25:16 -04002127 /* get encoding parameters */
2128 opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
2129 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002130
Nicolas Pena088ca032017-09-01 13:25:16 -04002131 if (l_tcp->POC) {
2132 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
2133 l_max_prec, l_max_res, l_dx_min, l_dy_min);
2134 } else {
2135 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
2136 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
2137 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002138}
2139
Nicolas Pena088ca032017-09-01 13:25:16 -04002140OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
2141{
2142 switch (pi->poc.prg) {
2143 case OPJ_LRCP:
2144 return opj_pi_next_lrcp(pi);
2145 case OPJ_RLCP:
2146 return opj_pi_next_rlcp(pi);
2147 case OPJ_RPCL:
2148 return opj_pi_next_rpcl(pi);
2149 case OPJ_PCRL:
2150 return opj_pi_next_pcrl(pi);
2151 case OPJ_CPRL:
2152 return opj_pi_next_cprl(pi);
2153 case OPJ_PROG_UNKNOWN:
2154 return OPJ_FALSE;
2155 }
2156
2157 return OPJ_FALSE;
John Abd-El-Malek5110c472014-05-17 22:33:34 -07002158}