John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1 | /* |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 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 |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 4 | * party and contributor rights, including patent rights, and no such rights |
| 5 | * are granted under this license. |
| 6 | * |
| 7 | * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium |
| 8 | * Copyright (c) 2002-2014, Professor Benoit Macq |
| 9 | * Copyright (c) 2001-2003, David Janssens |
| 10 | * Copyright (c) 2002-2003, Yannick Verschueren |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 11 | * Copyright (c) 2003-2007, Francois-Olivier Devaux |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 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. |
| 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 39 | #define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_UINT32 |
| 40 | |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 41 | #include "opj_includes.h" |
| 42 | |
| 43 | /** @defgroup PI PI - Implementation of a packet iterator */ |
| 44 | /*@{*/ |
| 45 | |
| 46 | /** @name Local static functions */ |
| 47 | /*@{*/ |
| 48 | |
| 49 | /** |
| 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 |
| 53 | */ |
| 54 | static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi); |
| 55 | /** |
| 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 |
| 59 | */ |
| 60 | static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi); |
| 61 | /** |
| 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 |
| 65 | */ |
| 66 | static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi); |
| 67 | /** |
| 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 |
| 71 | */ |
| 72 | static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi); |
| 73 | /** |
| 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 |
| 77 | */ |
| 78 | static 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 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. |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 93 | */ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 94 | static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp, |
| 95 | OPJ_UINT32 p_tileno, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 96 | OPJ_UINT32 p_tx0, |
| 97 | OPJ_UINT32 p_tx1, |
| 98 | OPJ_UINT32 p_ty0, |
| 99 | OPJ_UINT32 p_ty1, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 100 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 104 | |
| 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 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. |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 119 | */ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 120 | static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp, |
| 121 | OPJ_UINT32 p_num_comps, |
| 122 | OPJ_UINT32 p_tileno, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 123 | OPJ_UINT32 p_tx0, |
| 124 | OPJ_UINT32 p_tx1, |
| 125 | OPJ_UINT32 p_ty0, |
| 126 | OPJ_UINT32 p_ty1, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 127 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Gets the encoding parameters needed to update the coding parameters and all the pocs. |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 133 | * |
| 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 145 | */ |
| 146 | static void opj_get_encoding_parameters(const opj_image_t *p_image, |
| 147 | const opj_cp_t *p_cp, |
| 148 | OPJ_UINT32 tileno, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 149 | OPJ_UINT32 * p_tx0, |
| 150 | OPJ_UINT32 * p_tx1, |
| 151 | OPJ_UINT32 * p_ty0, |
| 152 | OPJ_UINT32 * p_ty1, |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 153 | OPJ_UINT32 * p_dx_min, |
| 154 | OPJ_UINT32 * p_dy_min, |
| 155 | OPJ_UINT32 * p_max_prec, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 156 | OPJ_UINT32 * p_max_res); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 157 | |
| 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 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. |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 177 | */ |
| 178 | static void opj_get_all_encoding_parameters(const opj_image_t *p_image, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 179 | const opj_cp_t *p_cp, |
| 180 | OPJ_UINT32 tileno, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 181 | OPJ_UINT32 * p_tx0, |
| 182 | OPJ_UINT32 * p_tx1, |
| 183 | OPJ_UINT32 * p_ty0, |
| 184 | OPJ_UINT32 * p_ty1, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 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); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 190 | /** |
| 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 193 | * |
| 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 197 | * @param manager Event manager |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 198 | */ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 199 | static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image, |
| 200 | const opj_cp_t *p_cp, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 201 | OPJ_UINT32 tileno, |
| 202 | opj_event_mgr_t* manager); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 203 | /** |
| 204 | * FIXME DOC |
| 205 | */ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 206 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 210 | /** |
| 211 | * FIXME DOC |
| 212 | */ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 213 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * FIXME DOC |
| 220 | */ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 221 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 226 | |
| 227 | /*@}*/ |
| 228 | |
| 229 | /*@}*/ |
| 230 | |
| 231 | /* |
| 232 | ========================================================== |
| 233 | local functions |
| 234 | ========================================================== |
| 235 | */ |
| 236 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 237 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 242 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 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"); |
| 247 | return OPJ_FALSE; |
| 248 | } |
| 249 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 250 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 280 | opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 281 | return OPJ_FALSE; |
| 282 | } |
| 283 | if (!pi->include[index]) { |
| 284 | pi->include[index] = 1; |
| 285 | return OPJ_TRUE; |
| 286 | } |
| 287 | LABEL_SKIP: |
| 288 | ; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return OPJ_FALSE; |
| 295 | } |
| 296 | |
| 297 | static 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 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"); |
| 307 | return OPJ_FALSE; |
| 308 | } |
| 309 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 310 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 333 | opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 334 | return OPJ_FALSE; |
| 335 | } |
| 336 | if (!pi->include[index]) { |
| 337 | pi->include[index] = 1; |
| 338 | return OPJ_TRUE; |
| 339 | } |
| 340 | LABEL_SKIP: |
| 341 | ; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return OPJ_FALSE; |
| 348 | } |
| 349 | |
| 350 | static 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 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"); |
| 360 | return OPJ_FALSE; |
| 361 | } |
| 362 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 363 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 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))) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 402 | for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { |
| 403 | OPJ_UINT32 levelno; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 404 | OPJ_UINT32 trx0, try0; |
| 405 | OPJ_UINT32 trx1, try1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 406 | OPJ_UINT32 rpx, rpy; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 407 | OPJ_UINT32 prci, prcj; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 408 | 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 Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 414 | |
| 415 | if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx || |
| 416 | (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 417 | continue; |
| 418 | } |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 419 | |
| 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 428 | rpx = res->pdx + levelno; |
| 429 | rpy = res->pdy + levelno; |
| 430 | |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 431 | if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx || |
| 432 | (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 433 | continue; |
| 434 | } |
| 435 | |
| 436 | /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */ |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 437 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 440 | continue; |
| 441 | } |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 442 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 445 | 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 Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 456 | prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x, |
| 457 | ((OPJ_UINT64)comp->dx << levelno)), res->pdx) |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 458 | - opj_uint_floordivpow2(trx0, res->pdx); |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 459 | prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y, |
| 460 | ((OPJ_UINT64)comp->dy << levelno)), res->pdy) |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 461 | - opj_uint_floordivpow2(try0, res->pdy); |
| 462 | pi->precno = prci + prcj * res->pw; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 463 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 470 | opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 471 | return OPJ_FALSE; |
| 472 | } |
| 473 | if (!pi->include[index]) { |
| 474 | pi->include[index] = 1; |
| 475 | return OPJ_TRUE; |
| 476 | } |
| 477 | LABEL_SKIP: |
| 478 | ; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return OPJ_FALSE; |
| 486 | } |
| 487 | |
| 488 | static 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 494 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 501 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 536 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 540 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 545 | OPJ_UINT32 trx0, try0; |
| 546 | OPJ_UINT32 trx1, try1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 547 | OPJ_UINT32 rpx, rpy; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 548 | OPJ_UINT32 prci, prcj; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 549 | res = &comp->resolutions[pi->resno]; |
| 550 | levelno = comp->numresolutions - 1 - pi->resno; |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 551 | |
| 552 | if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx || |
| 553 | (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 554 | continue; |
| 555 | } |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 556 | |
| 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 565 | rpx = res->pdx + levelno; |
| 566 | rpy = res->pdy + levelno; |
| 567 | |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 568 | if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx || |
| 569 | (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 570 | continue; |
| 571 | } |
| 572 | |
| 573 | /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */ |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 574 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 577 | continue; |
| 578 | } |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 579 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 582 | 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 Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 593 | prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x, |
| 594 | ((OPJ_UINT64)comp->dx << levelno)), res->pdx) |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 595 | - opj_uint_floordivpow2(trx0, res->pdx); |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 596 | prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y, |
| 597 | ((OPJ_UINT64)comp->dy << levelno)), res->pdy) |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 598 | - opj_uint_floordivpow2(try0, res->pdy); |
| 599 | pi->precno = prci + prcj * res->pw; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 600 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 607 | opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 608 | return OPJ_FALSE; |
| 609 | } |
| 610 | if (!pi->include[index]) { |
| 611 | pi->include[index] = 1; |
| 612 | return OPJ_TRUE; |
| 613 | } |
| 614 | LABEL_SKIP: |
| 615 | ; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | return OPJ_FALSE; |
| 623 | } |
| 624 | |
| 625 | static 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 631 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 638 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 673 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 677 | for (pi->resno = pi->poc.resno0; |
| 678 | pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { |
| 679 | OPJ_UINT32 levelno; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 680 | OPJ_UINT32 trx0, try0; |
| 681 | OPJ_UINT32 trx1, try1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 682 | OPJ_UINT32 rpx, rpy; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 683 | OPJ_UINT32 prci, prcj; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 684 | res = &comp->resolutions[pi->resno]; |
| 685 | levelno = comp->numresolutions - 1 - pi->resno; |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 686 | |
| 687 | if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx || |
| 688 | (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 689 | continue; |
| 690 | } |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 691 | |
| 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 700 | rpx = res->pdx + levelno; |
| 701 | rpy = res->pdy + levelno; |
| 702 | |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 703 | if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx || |
| 704 | (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 705 | continue; |
| 706 | } |
| 707 | |
| 708 | /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */ |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 709 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 712 | continue; |
| 713 | } |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 714 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 717 | 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 Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 728 | prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x, |
| 729 | ((OPJ_UINT64)comp->dx << levelno)), res->pdx) |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 730 | - opj_uint_floordivpow2(trx0, res->pdx); |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 731 | prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y, |
| 732 | ((OPJ_UINT64)comp->dy << levelno)), res->pdy) |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 733 | - opj_uint_floordivpow2(try0, res->pdy); |
| 734 | pi->precno = (OPJ_UINT32)(prci + prcj * res->pw); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 735 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 742 | opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include"); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 743 | return OPJ_FALSE; |
| 744 | } |
| 745 | if (!pi->include[index]) { |
| 746 | pi->include[index] = 1; |
| 747 | return OPJ_TRUE; |
| 748 | } |
| 749 | LABEL_SKIP: |
| 750 | ; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | return OPJ_FALSE; |
| 758 | } |
| 759 | |
| 760 | static void opj_get_encoding_parameters(const opj_image_t *p_image, |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 761 | const opj_cp_t *p_cp, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 762 | OPJ_UINT32 p_tileno, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 763 | OPJ_UINT32 * p_tx0, |
| 764 | OPJ_UINT32 * p_tx1, |
| 765 | OPJ_UINT32 * p_ty0, |
| 766 | OPJ_UINT32 * p_ty1, |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 767 | OPJ_UINT32 * p_dx_min, |
| 768 | OPJ_UINT32 * p_dy_min, |
| 769 | OPJ_UINT32 * p_max_prec, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 770 | OPJ_UINT32 * p_max_res) |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 771 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 772 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 778 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 779 | /* position in x and y of tile */ |
| 780 | OPJ_UINT32 p, q; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 781 | |
Lei Zhang | 3dd6ef0 | 2019-07-03 18:40:49 +0000 | [diff] [blame] | 782 | /* non-corrected (in regard to image offset) tile offset */ |
| 783 | OPJ_UINT32 l_tx0, l_ty0; |
| 784 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 785 | /* preconditions */ |
| 786 | assert(p_cp != 00); |
| 787 | assert(p_image != 00); |
| 788 | assert(p_tileno < p_cp->tw * p_cp->th); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 789 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 790 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 794 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 795 | /* 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 FANG | 7dba986 | 2015-02-27 10:00:00 -0800 | [diff] [blame] | 798 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 799 | /* find extent of tile */ |
Lei Zhang | 3dd6ef0 | 2019-07-03 18:40:49 +0000 | [diff] [blame] | 800 | l_tx0 = p_cp->tx0 + p * |
| 801 | p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */ |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 802 | *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 Zhang | 3dd6ef0 | 2019-07-03 18:40:49 +0000 | [diff] [blame] | 804 | l_ty0 = p_cp->ty0 + q * |
| 805 | p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */ |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 806 | *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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 808 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 809 | /* max precision is 0 (can only grow) */ |
| 810 | *p_max_prec = 0; |
| 811 | *p_max_res = 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 812 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 813 | /* take the largest value for dx_min and dy_min */ |
| 814 | *p_dx_min = 0x7fffffff; |
| 815 | *p_dy_min = 0x7fffffff; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 816 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 817 | for (compno = 0; compno < p_image->numcomps; ++compno) { |
| 818 | /* arithmetic variables to calculate */ |
| 819 | OPJ_UINT32 l_level_no; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 820 | OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1; |
| 821 | OPJ_UINT32 l_px0, l_py0, l_px1, py1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 822 | OPJ_UINT32 l_pdx, l_pdy; |
| 823 | OPJ_UINT32 l_pw, l_ph; |
| 824 | OPJ_UINT32 l_product; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 825 | OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 826 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 827 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 831 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 832 | if (l_tccp->numresolutions > *p_max_res) { |
| 833 | *p_max_res = l_tccp->numresolutions; |
| 834 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 835 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 836 | /* use custom size for precincts */ |
| 837 | for (resno = 0; resno < l_tccp->numresolutions; ++resno) { |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 838 | OPJ_UINT64 l_dx, l_dy; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 839 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 840 | /* precinct width and height */ |
| 841 | l_pdx = l_tccp->prcw[resno]; |
| 842 | l_pdy = l_tccp->prch[resno]; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 843 | |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 844 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 848 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 849 | /* take the minimum size for dx for each comp and resolution */ |
Lei Zhang | ee35a50 | 2024-05-30 17:31:47 +0000 | [diff] [blame] | 850 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 856 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 857 | /* various calculations of extents */ |
| 858 | l_level_no = l_tccp->numresolutions - 1 - resno; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 859 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 860 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 864 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 865 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 868 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 869 | py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 870 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 871 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 873 | |
| 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 | |
| 887 | static void opj_get_all_encoding_parameters(const opj_image_t *p_image, |
| 888 | const opj_cp_t *p_cp, |
| 889 | OPJ_UINT32 tileno, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 890 | OPJ_UINT32 * p_tx0, |
| 891 | OPJ_UINT32 * p_tx1, |
| 892 | OPJ_UINT32 * p_ty0, |
| 893 | OPJ_UINT32 * p_ty1, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 894 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 934 | *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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 936 | l_ty0 = p_cp->ty0 + q * |
| 937 | p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */ |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 938 | *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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 940 | |
| 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 Zhang | c06d7e7 | 2022-06-10 22:07:55 +0000 | [diff] [blame] | 950 | /* arithmetic variables to calculate*/ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 951 | OPJ_UINT32 l_level_no; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 952 | OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1; |
| 953 | OPJ_UINT32 l_px0, l_py0, l_px1, py1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 954 | OPJ_UINT32 l_product; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 955 | OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 956 | OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph; |
| 957 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 958 | lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 959 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 960 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 964 | |
| 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 979 | if (lResolutionPtr) { |
| 980 | *lResolutionPtr++ = l_pdx; |
| 981 | *lResolutionPtr++ = l_pdy; |
| 982 | } |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 983 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 987 | *p_dx_min = opj_uint_min(*p_dx_min, l_dx); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 988 | } |
| 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 992 | *p_dy_min = opj_uint_min(*p_dy_min, l_dy); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /* various calculations of extents*/ |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 996 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1010 | l_product = l_pw * l_ph; |
| 1011 | |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1012 | /* update precision*/ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1013 | if (l_product > *p_max_prec) { |
| 1014 | *p_max_prec = l_product; |
| 1015 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1016 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1017 | } |
| 1018 | ++l_tccp; |
| 1019 | ++l_img_comp; |
| 1020 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1023 | static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image, |
| 1024 | const opj_cp_t *cp, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1025 | OPJ_UINT32 tileno, |
| 1026 | opj_event_mgr_t* manager) |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1027 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1028 | /* loop*/ |
| 1029 | OPJ_UINT32 pino, compno; |
| 1030 | /* number of poc in the p_pi*/ |
| 1031 | OPJ_UINT32 l_poc_bound; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1032 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1033 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1037 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1038 | /* current packet iterator being allocated*/ |
| 1039 | opj_pi_iterator_t *l_current_pi = 00; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1040 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1041 | /* preconditions in debug*/ |
| 1042 | assert(cp != 00); |
| 1043 | assert(image != 00); |
| 1044 | assert(tileno < cp->tw * cp->th); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1045 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1046 | /* initializations*/ |
| 1047 | tcp = &cp->tcps[tileno]; |
| 1048 | l_poc_bound = tcp->numpocs + 1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1049 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1050 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1056 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1057 | l_current_pi = l_pi; |
| 1058 | for (pino = 0; pino < l_poc_bound ; ++pino) { |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1059 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1060 | l_current_pi->manager = manager; |
| 1061 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1062 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1068 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1069 | l_current_pi->numcomps = image->numcomps; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1070 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1071 | for (compno = 0; compno < image->numcomps; ++compno) { |
| 1072 | opj_pi_comp_t *comp = &l_current_pi->comps[compno]; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1073 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1074 | tccp = &tcp->tccps[compno]; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1075 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1076 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1082 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1083 | comp->numresolutions = tccp->numresolutions; |
| 1084 | } |
| 1085 | ++l_current_pi; |
| 1086 | } |
| 1087 | return l_pi; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1090 | static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp, |
| 1091 | OPJ_UINT32 p_tileno, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1092 | OPJ_UINT32 p_tx0, |
| 1093 | OPJ_UINT32 p_tx1, |
| 1094 | OPJ_UINT32 p_ty0, |
| 1095 | OPJ_UINT32 p_ty1, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1096 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1100 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1101 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1107 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1108 | /* number of pocs*/ |
| 1109 | OPJ_UINT32 l_poc_bound; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1110 | |
| 1111 | OPJ_ARG_NOT_USED(p_max_res); |
| 1112 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1113 | /* preconditions in debug*/ |
| 1114 | assert(p_cp != 00); |
| 1115 | assert(p_tileno < p_cp->tw * p_cp->th); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1116 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1117 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1121 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1122 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1125 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1126 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1131 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1132 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1136 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1137 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1144 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1145 | ++ 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1157 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1158 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1169 | static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp, |
| 1170 | OPJ_UINT32 p_num_comps, |
| 1171 | OPJ_UINT32 p_tileno, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1172 | OPJ_UINT32 p_tx0, |
| 1173 | OPJ_UINT32 p_tx1, |
| 1174 | OPJ_UINT32 p_ty0, |
| 1175 | OPJ_UINT32 p_ty1, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1176 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1180 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1181 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1189 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1190 | /* preconditions in debug*/ |
| 1191 | assert(p_cp != 00); |
| 1192 | assert(p_tileno < p_cp->tw * p_cp->th); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1193 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1194 | /* initializations*/ |
| 1195 | l_tcp = &p_cp->tcps [p_tileno]; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1196 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1197 | /* number of iterations in the loop */ |
| 1198 | l_poc_bound = l_tcp->numpocs + 1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1199 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1200 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1203 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1204 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1214 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1218 | l_current_poc->dx = p_dx_min; |
| 1219 | l_current_poc->dy = p_dy_min; |
| 1220 | ++ l_current_poc; |
| 1221 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1224 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1228 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1229 | /* loop*/ |
| 1230 | OPJ_UINT32 pino; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1231 | |
Lei Zhang | c06d7e7 | 2022-06-10 22:07:55 +0000 | [diff] [blame] | 1232 | /* encoding parameters to set*/ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1233 | OPJ_UINT32 l_bound; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1234 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1235 | opj_pi_iterator_t * l_current_pi = 00; |
| 1236 | opj_poc_t* l_current_poc = 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1237 | |
| 1238 | OPJ_ARG_NOT_USED(p_max_res); |
| 1239 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1240 | /* preconditions in debug*/ |
| 1241 | assert(p_pi != 00); |
| 1242 | assert(p_tcp != 00); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1243 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1244 | /* initializations*/ |
| 1245 | l_bound = p_tcp->numpocs + 1; |
| 1246 | l_current_pi = p_pi; |
| 1247 | l_current_poc = p_tcp->pocs; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1248 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1249 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1252 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1253 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1271 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1275 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1276 | /* loop*/ |
| 1277 | OPJ_UINT32 pino; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1278 | |
Lei Zhang | c06d7e7 | 2022-06-10 22:07:55 +0000 | [diff] [blame] | 1279 | /* encoding parameters to set*/ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1280 | OPJ_UINT32 l_bound; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1281 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1282 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1286 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1287 | /* initializations*/ |
| 1288 | l_bound = p_tcp->numpocs + 1; |
| 1289 | l_current_pi = p_pi; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1290 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1291 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1308 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1313 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1314 | OPJ_INT32 i; |
| 1315 | opj_tcp_t *tcps = &cp->tcps[tileno]; |
| 1316 | opj_poc_t *tcp = &tcps->pocs[pino]; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1317 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1318 | if (pos >= 0) { |
Lei Zhang | c06d7e7 | 2022-06-10 22:07:55 +0000 | [diff] [blame] | 1319 | for (i = pos; i >= 0; i--) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1320 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | |
| 1392 | /* |
| 1393 | ========================================================== |
| 1394 | Packet iterator interface |
| 1395 | ========================================================== |
| 1396 | */ |
| 1397 | opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1398 | opj_cp_t *p_cp, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1399 | OPJ_UINT32 p_tile_no, |
| 1400 | opj_event_mgr_t* manager) |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1401 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1402 | OPJ_UINT32 numcomps = p_image->numcomps; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1403 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1404 | /* loop */ |
| 1405 | OPJ_UINT32 pino; |
| 1406 | OPJ_UINT32 compno, resno; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1407 | |
Lei Zhang | c06d7e7 | 2022-06-10 22:07:55 +0000 | [diff] [blame] | 1408 | /* to store w, h, dx and dy for all components and resolutions */ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1409 | OPJ_UINT32 * l_tmp_data; |
| 1410 | OPJ_UINT32 ** l_tmp_ptr; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1411 | |
Lei Zhang | c06d7e7 | 2022-06-10 22:07:55 +0000 | [diff] [blame] | 1412 | /* encoding parameters to set */ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1413 | OPJ_UINT32 l_max_res; |
| 1414 | OPJ_UINT32 l_max_prec; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1415 | OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1416 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1420 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1421 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1429 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1430 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1434 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1435 | /* initializations */ |
| 1436 | l_tcp = &p_cp->tcps[p_tile_no]; |
| 1437 | l_bound = l_tcp->numpocs + 1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1438 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1439 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1453 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1454 | /* memory allocation for pi */ |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1455 | l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1456 | if (!l_pi) { |
| 1457 | opj_free(l_tmp_data); |
| 1458 | opj_free(l_tmp_ptr); |
| 1459 | return 00; |
| 1460 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1461 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1462 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1472 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1473 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1478 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1479 | /* set values for first packet iterator */ |
| 1480 | l_current_pi = l_pi; |
JUN FANG | 83ad95c | 2015-07-13 06:34:20 -0700 | [diff] [blame] | 1481 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1482 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1491 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1492 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1498 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1499 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1503 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1504 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1508 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1509 | /*l_current_pi->dx = l_img_comp->dx;*/ |
| 1510 | /*l_current_pi->dy = l_img_comp->dy;*/ |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1511 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1512 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1516 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1517 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1522 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1523 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1539 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1540 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1544 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1545 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1555 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1556 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1596 | OPJ_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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1618 | |
| 1619 | opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image, |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1620 | opj_cp_t *p_cp, |
| 1621 | OPJ_UINT32 p_tile_no, |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1622 | J2K_T2_MODE p_t2_mode, |
| 1623 | opj_event_mgr_t* manager) |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1624 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1625 | OPJ_UINT32 numcomps = p_image->numcomps; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1626 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1627 | /* loop*/ |
| 1628 | OPJ_UINT32 pino; |
| 1629 | OPJ_UINT32 compno, resno; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1630 | |
Lei Zhang | c06d7e7 | 2022-06-10 22:07:55 +0000 | [diff] [blame] | 1631 | /* to store w, h, dx and dy for all components and resolutions*/ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1632 | OPJ_UINT32 * l_tmp_data; |
| 1633 | OPJ_UINT32 ** l_tmp_ptr; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1634 | |
Lei Zhang | c06d7e7 | 2022-06-10 22:07:55 +0000 | [diff] [blame] | 1635 | /* encoding parameters to set*/ |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1636 | OPJ_UINT32 l_max_res; |
| 1637 | OPJ_UINT32 l_max_prec; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1638 | OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1639 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1643 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1644 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1652 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1653 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1657 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1658 | /* initializations*/ |
| 1659 | l_tcp = &p_cp->tcps[p_tile_no]; |
| 1660 | l_bound = l_tcp->numpocs + 1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1661 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1662 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1668 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1669 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1675 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1676 | /* memory allocation for pi*/ |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1677 | l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1678 | if (!l_pi) { |
| 1679 | opj_free(l_tmp_data); |
| 1680 | opj_free(l_tmp_ptr); |
| 1681 | return 00; |
| 1682 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1683 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1684 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1690 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1691 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1694 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1695 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1700 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1701 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1704 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1705 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1715 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1716 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1730 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1731 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1735 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1736 | l_current_comp->dx = l_img_comp->dx; |
| 1737 | l_current_comp->dy = l_img_comp->dy; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1738 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1739 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1747 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1748 | ++l_current_comp; |
| 1749 | ++l_img_comp; |
| 1750 | ++l_tccp; |
| 1751 | } |
| 1752 | ++l_current_pi; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1753 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1754 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1758 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1759 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1769 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1770 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1774 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1775 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1789 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1790 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1800 | |
Bo Xu | d53e6fd | 2014-09-30 11:12:05 -0700 | [diff] [blame] | 1801 | if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1802 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1808 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1809 | return l_pi; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1812 | void 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1819 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1820 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1825 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1826 | prog = opj_j2k_convert_progression_order(tcp->prg); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1827 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1828 | pi[pino].first = 1; |
| 1829 | pi[pino].poc.prg = tcp->prg; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1830 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1831 | if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) && |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1832 | !OPJ_IS_IMF(cp->rsiz) && |
| 1833 | (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1834 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1842 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1846 | } 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1869 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1873 | break; |
| 1874 | } |
| 1875 | break; |
| 1876 | } |
| 1877 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1878 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1879 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1912 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1916 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 1947 | 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 Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 1951 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 2037 | pi[pino].poc.ty0 = tcp->ty0_t; |
| 2038 | pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2039 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 2047 | pi[pino].poc.ty0 = tcp->ty0_t; |
| 2048 | pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2049 | 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 Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 2055 | pi[pino].poc.tx0 = tcp->tx0_t; |
| 2056 | pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2057 | tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1; |
| 2058 | } |
| 2059 | } else { |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 2060 | pi[pino].poc.tx0 = tcp->tx0_t; |
| 2061 | pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2062 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | void opj_pi_destroy(opj_pi_iterator_t *p_pi, |
| 2076 | OPJ_UINT32 p_nb_elements) |
| 2077 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2078 | OPJ_UINT32 compno, pino; |
| 2079 | opj_pi_iterator_t *l_current_pi = p_pi; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2080 | if (p_pi) { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2081 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2093 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2094 | ++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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | |
| 2106 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2107 | void 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2110 | { |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2111 | /* encoding parameters to set */ |
| 2112 | OPJ_UINT32 l_max_res; |
| 2113 | OPJ_UINT32 l_max_prec; |
Daniel Hosseinian | a81ff72 | 2021-02-17 20:56:08 +0000 | [diff] [blame] | 2114 | OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2115 | OPJ_UINT32 l_dx_min, l_dy_min; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2116 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2117 | /* pointers */ |
| 2118 | opj_tcp_t *l_tcp = 00; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2119 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2120 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2124 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2125 | l_tcp = &(p_cp->tcps[p_tile_no]); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2126 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2127 | /* 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2130 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2131 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
Nicolas Pena | 088ca03 | 2017-09-01 13:25:16 -0400 | [diff] [blame] | 2140 | OPJ_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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 2158 | } |