1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "pi.h"
35 #include "int.h"
36 #include "opj_malloc.h"
37 #include "j2k.h"
38 /** @defgroup PI PI - Implementation of a packet iterator */
39 /*@{*/
40 
41 /** @name Local static functions */
42 /*@{*/
43 
44 /**
45 Get next packet in layer-resolution-component-precinct order.
46 @param pi packet iterator to modify
47 @return returns false if pi pointed to the last packet or else returns true
48 */
49 static bool pi_next_lrcp(opj_pi_iterator_t * pi);
50 /**
51 Get next packet in resolution-layer-component-precinct order.
52 @param pi packet iterator to modify
53 @return returns false if pi pointed to the last packet or else returns true
54 */
55 static bool pi_next_rlcp(opj_pi_iterator_t * pi);
56 /**
57 Get next packet in resolution-precinct-component-layer order.
58 @param pi packet iterator to modify
59 @return returns false if pi pointed to the last packet or else returns true
60 */
61 static bool pi_next_rpcl(opj_pi_iterator_t * pi);
62 /**
63 Get next packet in precinct-component-resolution-layer order.
64 @param pi packet iterator to modify
65 @return returns false if pi pointed to the last packet or else returns true
66 */
67 static bool pi_next_pcrl(opj_pi_iterator_t * pi);
68 /**
69 Get next packet in component-precinct-resolution-layer order.
70 @param pi packet iterator to modify
71 @return returns false if pi pointed to the last packet or else returns true
72 */
73 static bool pi_next_cprl(opj_pi_iterator_t * pi);
74 
75 /**
76  * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
77  *
78  * @param        p_cp                the coding parameters to modify
79  * @param        p_tileno        the tile index being concerned.
80  * @param        p_tx0                X0 parameter for the tile
81  * @param        p_tx1                X1 parameter for the tile
82  * @param        p_ty0                Y0 parameter for the tile
83  * @param        p_ty1                Y1 parameter for the tile
84  * @param        p_max_prec        the maximum precision for all the bands of the tile
85  * @param        p_max_res        the maximum number of resolutions for all the poc inside the tile.
86  * @param        dx_min                the minimum dx of all the components of all the resolutions for the tile.
87  * @param        dy_min                the minimum dy of all the components of all the resolutions for the tile.
88  */
89 void pi_update_encode_poc_and_final (
90                                                                          opj_cp_t *p_cp,
91                                                                          OPJ_UINT32 p_tileno,
92                                                                          OPJ_INT32 p_tx0,
93                                                                          OPJ_INT32 p_tx1,
94                                                                          OPJ_INT32 p_ty0,
95                                                                          OPJ_INT32 p_ty1,
96                                                                          OPJ_UINT32 p_max_prec,
97                                                                          OPJ_UINT32 p_max_res,
98                                      OPJ_UINT32 p_dx_min,
99                                                                          OPJ_UINT32 p_dy_min);
100 
101 /**
102  * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
103  *
104  * @param        p_cp                the coding parameters to modify
105  * @param        p_tileno        the tile index being concerned.
106  * @param        p_tx0                X0 parameter for the tile
107  * @param        p_tx1                X1 parameter for the tile
108  * @param        p_ty0                Y0 parameter for the tile
109  * @param        p_ty1                Y1 parameter for the tile
110  * @param        p_max_prec        the maximum precision for all the bands of the tile
111  * @param        p_max_res        the maximum number of resolutions for all the poc inside the tile.
112  * @param        dx_min                the minimum dx of all the components of all the resolutions for the tile.
113  * @param        dy_min                the minimum dy of all the components of all the resolutions for the tile.
114  */
115 void pi_update_encode_not_poc (
116                                                                 opj_cp_t *p_cp,
117                                                                 OPJ_UINT32 p_num_comps,
118                                                                 OPJ_UINT32 p_tileno,
119                                                                 OPJ_INT32 p_tx0,
120                                                                 OPJ_INT32 p_tx1,
121                                                                 OPJ_INT32 p_ty0,
122                                                                 OPJ_INT32 p_ty1,
123                                                                 OPJ_UINT32 p_max_prec,
124                                                                 OPJ_UINT32 p_max_res,
125                                 OPJ_UINT32 p_dx_min,
126                                                                 OPJ_UINT32 p_dy_min);
127 
128 /**
129  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
130  *
131  * @param        p_image                        the image being encoded.
132  * @param        p_cp                        the coding parameters.
133  * @param        tileno                        the tile index of the tile being encoded.
134  * @param        p_tx0                        pointer that will hold the X0 parameter for the tile
135  * @param        p_tx1                        pointer that will hold the X1 parameter for the tile
136  * @param        p_ty0                        pointer that will hold the Y0 parameter for the tile
137  * @param        p_ty1                        pointer that will hold the Y1 parameter for the tile
138  * @param        p_max_prec                pointer that will hold the the maximum precision for all the bands of the tile
139  * @param        p_max_res                pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
140  * @param        dx_min                        pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
141  * @param        dy_min                        pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
142  */
143 void get_encoding_parameters(
144                                                                 const opj_image_t *p_image,
145                                                                 const opj_cp_t *p_cp,
146                                                                 OPJ_UINT32  tileno,
147                                                                 OPJ_INT32  * p_tx0,
148                                                                 OPJ_INT32 * p_tx1,
149                                                                 OPJ_INT32 * p_ty0,
150                                                                 OPJ_INT32 * p_ty1,
151                                                                 OPJ_UINT32 * p_dx_min,
152                                                                 OPJ_UINT32 * p_dy_min,
153                                                                 OPJ_UINT32 * p_max_prec,
154                                                                 OPJ_UINT32 * p_max_res
155                                                         );
156 
157 /**
158  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
159  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
160  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
161  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
162  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
163  *
164  * @param        p_image                        the image being encoded.
165  * @param        p_cp                        the coding parameters.
166  * @param        tileno                        the tile index of the tile being encoded.
167  * @param        p_tx0                        pointer that will hold the X0 parameter for the tile
168  * @param        p_tx1                        pointer that will hold the X1 parameter for the tile
169  * @param        p_ty0                        pointer that will hold the Y0 parameter for the tile
170  * @param        p_ty1                        pointer that will hold the Y1 parameter for the tile
171  * @param        p_max_prec                pointer that will hold the the maximum precision for all the bands of the tile
172  * @param        p_max_res                pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
173  * @param        dx_min                        pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
174  * @param        dy_min                        pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
175  * @param        p_resolutions        pointer to an area corresponding to the one described above.
176  */
177 void get_all_encoding_parameters(
178                                                                 const opj_image_t *p_image,
179                                                                 const opj_cp_t *p_cp,
180                                                                 OPJ_UINT32 tileno,
181                                                                 OPJ_INT32 * p_tx0,
182                                                                 OPJ_INT32 * p_tx1,
183                                                                 OPJ_INT32 * p_ty0,
184                                                                 OPJ_INT32 * p_ty1,
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
190                                                         );
191 /**
192  * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
193  * No other data is set. The include section of the packet  iterator is not allocated.
194  *
195  * @param        p_image                the image used to initialize the packet iterator (in fact only the number of components is relevant.
196  * @param        p_cp                the coding parameters.
197  * @param        p_tile_no        the index of the tile from which creating the packet iterator.
198  */
199 opj_pi_iterator_t * pi_create(
200                                                                 const opj_image_t *image,
201                                                                 const opj_cp_t *cp,
202                                                                 OPJ_UINT32 tileno
203                                                         );
204 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
205 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
206 
207 
208 /*@}*/
209 
210 /*@}*/
211 
212 /*
213 ==========================================================
214    local functions
215 ==========================================================
216 */
217 
pi_next_lrcp(opj_pi_iterator_t * pi)218 static bool pi_next_lrcp(opj_pi_iterator_t * pi) {
219         opj_pi_comp_t *comp = 00;
220         opj_pi_resolution_t *res = 00;
221         OPJ_UINT32 index = 0;
222 
223         if (!pi->first) {
224                 comp = &pi->comps[pi->compno];
225                 goto LABEL_SKIP;
226         } else {
227                 pi->first = 0;
228         }
229 
230         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
231                 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
232                 pi->resno++) {
233                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
234                                 comp = &pi->comps[pi->compno];
235                                 if (pi->resno >= comp->numresolutions) {
236                                         continue;
237                                 }
238                                 res = &comp->resolutions[pi->resno];
239                                 if (!pi->tp_on){
240                                         pi->poc.precno1 = res->pw * res->ph;
241                                 }
242                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
243                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
244                                         if (!pi->include[index]) {
245                                                 pi->include[index] = 1;
246                                                 return true;
247                                         }
248 LABEL_SKIP:;
249                                 }
250                         }
251                 }
252         }
253 
254         return false;
255 }
256 
pi_next_rlcp(opj_pi_iterator_t * pi)257 static bool pi_next_rlcp(opj_pi_iterator_t * pi) {
258         opj_pi_comp_t *comp = 00;
259         opj_pi_resolution_t *res = 00;
260         OPJ_UINT32 index = 0;
261 
262         if (!pi->first) {
263                 comp = &pi->comps[pi->compno];
264                 goto LABEL_SKIP;
265         } else {
266                 pi->first = 0;
267         }
268 
269         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
270                 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
271                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
272                                 comp = &pi->comps[pi->compno];
273                                 if (pi->resno >= comp->numresolutions) {
274                                         continue;
275                                 }
276                                 res = &comp->resolutions[pi->resno];
277                                 if(!pi->tp_on){
278                                         pi->poc.precno1 = res->pw * res->ph;
279                                 }
280                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
281                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
282                                         if (!pi->include[index]) {
283                                                 pi->include[index] = 1;
284                                                 return true;
285                                         }
286 LABEL_SKIP:;
287                                 }
288                         }
289                 }
290         }
291 
292         return false;
293 }
294 
pi_next_rpcl(opj_pi_iterator_t * pi)295 static bool pi_next_rpcl(opj_pi_iterator_t * pi) {
296         opj_pi_comp_t *comp = 00;
297         opj_pi_resolution_t *res = 00;
298         OPJ_UINT32 index = 0;
299 
300         if (!pi->first) {
301                 goto LABEL_SKIP;
302         } else {
303                 OPJ_UINT32 compno, resno;
304                 pi->first = 0;
305                 pi->dx = 0;
306                 pi->dy = 0;
307                 for (compno = 0; compno < pi->numcomps; compno++) {
308                         comp = &pi->comps[compno];
309                         for (resno = 0; resno < comp->numresolutions; resno++) {
310                                 OPJ_UINT32 dx, dy;
311                                 res = &comp->resolutions[resno];
312                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
313                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
314                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
315                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
316                         }
317                 }
318         }
319 if (!pi->tp_on){
320                         pi->poc.ty0 = pi->ty0;
321                         pi->poc.tx0 = pi->tx0;
322                         pi->poc.ty1 = pi->ty1;
323                         pi->poc.tx1 = pi->tx1;
324                 }
325         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
326                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
327                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
328                                 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
329                                         OPJ_UINT32 levelno;
330                                         OPJ_INT32 trx0, try0;
331                                         OPJ_INT32  trx1, try1;
332                                         OPJ_UINT32  rpx, rpy;
333                                         OPJ_INT32  prci, prcj;
334                                         comp = &pi->comps[pi->compno];
335                                         if (pi->resno >= comp->numresolutions) {
336                                                 continue;
337                                         }
338                                         res = &comp->resolutions[pi->resno];
339                                         levelno = comp->numresolutions - 1 - pi->resno;
340                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
341                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
342                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
343                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
344                                         rpx = res->pdx + levelno;
345                                         rpy = res->pdy + levelno;
346                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
347                                                 continue;
348                                         }
349                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
350                                                 continue;
351                                         }
352 
353                                         if ((res->pw==0)||(res->ph==0)) continue;
354 
355                                         if ((trx0==trx1)||(try0==try1)) continue;
356 
357                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
358                                                  - int_floordivpow2(trx0, res->pdx);
359                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
360                                                  - int_floordivpow2(try0, res->pdy);
361                                         pi->precno = prci + prcj * res->pw;
362                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
363                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
364                                                 if (!pi->include[index]) {
365                                                         pi->include[index] = 1;
366                                                         return true;
367                                                 }
368 LABEL_SKIP:;
369                                         }
370                                 }
371                         }
372                 }
373         }
374 
375         return false;
376 }
377 
pi_next_pcrl(opj_pi_iterator_t * pi)378 static bool pi_next_pcrl(opj_pi_iterator_t * pi) {
379         opj_pi_comp_t *comp = 00;
380         opj_pi_resolution_t *res = 00;
381         OPJ_UINT32 index = 0;
382 
383         if (!pi->first) {
384                 comp = &pi->comps[pi->compno];
385                 goto LABEL_SKIP;
386         } else {
387                 OPJ_UINT32 compno, resno;
388                 pi->first = 0;
389                 pi->dx = 0;
390                 pi->dy = 0;
391                 for (compno = 0; compno < pi->numcomps; compno++) {
392                         comp = &pi->comps[compno];
393                         for (resno = 0; resno < comp->numresolutions; resno++) {
394                                 OPJ_UINT32 dx, dy;
395                                 res = &comp->resolutions[resno];
396                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
397                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
398                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
399                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
400                         }
401                 }
402         }
403         if (!pi->tp_on){
404                         pi->poc.ty0 = pi->ty0;
405                         pi->poc.tx0 = pi->tx0;
406                         pi->poc.ty1 = pi->ty1;
407                         pi->poc.tx1 = pi->tx1;
408                 }
409         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
410                 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
411                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
412                                 comp = &pi->comps[pi->compno];
413                                 // TODO
414                                 for (pi->resno = pi->poc.resno0; pi->resno < uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
415                                         OPJ_UINT32 levelno;
416                                         OPJ_INT32 trx0, try0;
417                                         OPJ_INT32 trx1, try1;
418                                         OPJ_UINT32 rpx, rpy;
419                                         OPJ_INT32 prci, prcj;
420                                         res = &comp->resolutions[pi->resno];
421                                         levelno = comp->numresolutions - 1 - pi->resno;
422                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
423                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
424                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
425                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
426                                         rpx = res->pdx + levelno;
427                                         rpy = res->pdy + levelno;
428                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
429                                                 continue;
430                                         }
431                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
432                                                 continue;
433                                         }
434 
435                                         if ((res->pw==0)||(res->ph==0)) continue;
436 
437                                         if ((trx0==trx1)||(try0==try1)) continue;
438 
439                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
440                                                  - int_floordivpow2(trx0, res->pdx);
441                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
442                                                  - int_floordivpow2(try0, res->pdy);
443                                         pi->precno = prci + prcj * res->pw;
444                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
445                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
446                                                 if (!pi->include[index]) {
447                                                         pi->include[index] = 1;
448                                                         return true;
449                                                 }
450 LABEL_SKIP:;
451                                         }
452                                 }
453                         }
454                 }
455         }
456 
457         return false;
458 }
459 
pi_next_cprl(opj_pi_iterator_t * pi)460 static bool pi_next_cprl(opj_pi_iterator_t * pi) {
461         opj_pi_comp_t *comp = 00;
462         opj_pi_resolution_t *res = 00;
463         OPJ_UINT32 index = 0;
464 
465         if (!pi->first) {
466                 comp = &pi->comps[pi->compno];
467                 goto LABEL_SKIP;
468         } else {
469                 pi->first = 0;
470         }
471 
472         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
473                 OPJ_UINT32 resno;
474                 comp = &pi->comps[pi->compno];
475                 pi->dx = 0;
476                 pi->dy = 0;
477                 for (resno = 0; resno < comp->numresolutions; resno++) {
478                         OPJ_UINT32 dx, dy;
479                         res = &comp->resolutions[resno];
480                         dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
481                         dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
482                         pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
483                         pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
484                 }
485                 if (!pi->tp_on){
486                         pi->poc.ty0 = pi->ty0;
487                         pi->poc.tx0 = pi->tx0;
488                         pi->poc.ty1 = pi->ty1;
489                         pi->poc.tx1 = pi->tx1;
490                 }
491                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
492                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
493                                 // TODO
494                                 for (pi->resno = pi->poc.resno0; pi->resno < uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
495                                         OPJ_UINT32 levelno;
496                                         OPJ_INT32 trx0, try0;
497                                         OPJ_INT32 trx1, try1;
498                                         OPJ_UINT32 rpx, rpy;
499                                         OPJ_INT32 prci, prcj;
500                                         res = &comp->resolutions[pi->resno];
501                                         levelno = comp->numresolutions - 1 - pi->resno;
502                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
503                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
504                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
505                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
506                                         rpx = res->pdx + levelno;
507                                         rpy = res->pdy + levelno;
508                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
509                                                 continue;
510                                         }
511                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
512                                                 continue;
513                                         }
514 
515                                         if ((res->pw==0)||(res->ph==0)) continue;
516 
517                                         if ((trx0==trx1)||(try0==try1)) continue;
518 
519                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
520                                                  - int_floordivpow2(trx0, res->pdx);
521                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
522                                                  - int_floordivpow2(try0, res->pdy);
523                                         pi->precno = prci + prcj * res->pw;
524                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
525                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
526                                                 if (!pi->include[index]) {
527                                                         pi->include[index] = 1;
528                                                         return true;
529                                                 }
530 LABEL_SKIP:;
531                                         }
532                                 }
533                         }
534                 }
535         }
536 
537         return false;
538 }
539 
540 /*
541 ==========================================================
542    Packet iterator interface
543 ==========================================================
544 */
pi_create_decode(opj_image_t * p_image,opj_cp_t * p_cp,OPJ_UINT32 p_tile_no)545 opj_pi_iterator_t *pi_create_decode(
546                                                                                 opj_image_t *p_image,
547                                                                                 opj_cp_t *p_cp,
548                                                                                 OPJ_UINT32 p_tile_no
549                                                                                 )
550 {
551         // loop
552         OPJ_UINT32 pino;
553         OPJ_UINT32 compno, resno;
554 
555         // to store w, h, dx and dy fro all components and resolutions
556         OPJ_UINT32 * l_tmp_data;
557         OPJ_UINT32 ** l_tmp_ptr;
558 
559         // encoding prameters to set
560         OPJ_UINT32 l_max_res;
561         OPJ_UINT32 l_max_prec;
562         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
563         OPJ_UINT32 l_dx_min,l_dy_min;
564         OPJ_UINT32 l_bound;
565         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
566         OPJ_UINT32 l_data_stride;
567 
568         // pointers
569         opj_pi_iterator_t *l_pi = 00;
570         opj_tcp_t *l_tcp = 00;
571         const opj_tccp_t *l_tccp = 00;
572         opj_pi_comp_t *l_current_comp = 00;
573         opj_image_comp_t * l_img_comp = 00;
574         opj_pi_iterator_t * l_current_pi = 00;
575         OPJ_UINT32 * l_encoding_value_ptr = 00;
576 
577         // preconditions in debug
578         assert(p_cp != 00);
579         assert(p_image != 00);
580         assert(p_tile_no < p_cp->tw * p_cp->th);
581 
582         // initializations
583         l_tcp = &p_cp->tcps[p_tile_no];
584         l_bound = l_tcp->numpocs+1;
585 
586         l_data_stride = 4 * J2K_MAXRLVLS;
587         l_tmp_data = (OPJ_UINT32*)opj_malloc(
588                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
589         if
590                 (! l_tmp_data)
591         {
592                 return 00;
593         }
594         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
595                 p_image->numcomps * sizeof(OPJ_UINT32 *));
596         if
597                 (! l_tmp_ptr)
598         {
599                 opj_free(l_tmp_data);
600                 return 00;
601         }
602 
603         // memory allocation for pi
604         l_pi = pi_create(p_image,p_cp,p_tile_no);
605         if
606                 (!l_pi)
607         {
608                 opj_free(l_tmp_data);
609                 opj_free(l_tmp_ptr);
610                 return 00;
611         }
612 
613         l_encoding_value_ptr = l_tmp_data;
614         // update pointer array
615         for
616                 (compno = 0; compno < p_image->numcomps; ++compno)
617         {
618                 l_tmp_ptr[compno] = l_encoding_value_ptr;
619                 l_encoding_value_ptr += l_data_stride;
620         }
621         // get encoding parameters
622         get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr);
623 
624         // step calculations
625         l_step_p = 1;
626         l_step_c = l_max_prec * l_step_p;
627         l_step_r = p_image->numcomps * l_step_c;
628         l_step_l = l_max_res * l_step_r;
629 
630         // set values for first packet iterator
631         l_current_pi = l_pi;
632 
633         // memory allocation for include
634         l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
635         if
636                 (!l_current_pi->include)
637         {
638                 opj_free(l_tmp_data);
639                 opj_free(l_tmp_ptr);
640                 pi_destroy(l_pi, l_bound);
641                 return 00;
642         }
643         memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
644 
645         // special treatment for the first packet iterator
646         l_current_comp = l_current_pi->comps;
647         l_img_comp = p_image->comps;
648         l_tccp = l_tcp->tccps;
649 
650         l_current_pi->tx0 = l_tx0;
651         l_current_pi->ty0 = l_ty0;
652         l_current_pi->tx1 = l_tx1;
653         l_current_pi->ty1 = l_ty1;
654 
655         //l_current_pi->dx = l_img_comp->dx;
656         //l_current_pi->dy = l_img_comp->dy;
657 
658         l_current_pi->step_p = l_step_p;
659         l_current_pi->step_c = l_step_c;
660         l_current_pi->step_r = l_step_r;
661         l_current_pi->step_l = l_step_l;
662 
663         /* allocation for components and number of components has already been calculated by pi_create */
664         for
665                 (compno = 0; compno < l_current_pi->numcomps; ++compno)
666         {
667                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
668                 l_encoding_value_ptr = l_tmp_ptr[compno];
669 
670                 l_current_comp->dx = l_img_comp->dx;
671                 l_current_comp->dy = l_img_comp->dy;
672                 /* resolutions have already been initialized */
673                 for
674                         (resno = 0; resno < l_current_comp->numresolutions; resno++)
675                 {
676                         l_res->pdx = *(l_encoding_value_ptr++);
677                         l_res->pdy = *(l_encoding_value_ptr++);
678                         l_res->pw =  *(l_encoding_value_ptr++);
679                         l_res->ph =  *(l_encoding_value_ptr++);
680                         ++l_res;
681                 }
682                 ++l_current_comp;
683                 ++l_img_comp;
684                 ++l_tccp;
685         }
686         ++l_current_pi;
687 
688         for
689                 (pino = 1 ; pino<l_bound ; ++pino )
690         {
691                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
692                 opj_image_comp_t * l_img_comp = p_image->comps;
693                 l_tccp = l_tcp->tccps;
694 
695                 l_current_pi->tx0 = l_tx0;
696                 l_current_pi->ty0 = l_ty0;
697                 l_current_pi->tx1 = l_tx1;
698                 l_current_pi->ty1 = l_ty1;
699                 //l_current_pi->dx = l_dx_min;
700                 //l_current_pi->dy = l_dy_min;
701                 l_current_pi->step_p = l_step_p;
702                 l_current_pi->step_c = l_step_c;
703                 l_current_pi->step_r = l_step_r;
704                 l_current_pi->step_l = l_step_l;
705 
706                 /* allocation for components and number of components has already been calculated by pi_create */
707                 for
708                         (compno = 0; compno < l_current_pi->numcomps; ++compno)
709                 {
710                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
711                         l_encoding_value_ptr = l_tmp_ptr[compno];
712 
713                         l_current_comp->dx = l_img_comp->dx;
714                         l_current_comp->dy = l_img_comp->dy;
715                         /* resolutions have already been initialized */
716                         for
717                                 (resno = 0; resno < l_current_comp->numresolutions; resno++)
718                         {
719                                 l_res->pdx = *(l_encoding_value_ptr++);
720                                 l_res->pdy = *(l_encoding_value_ptr++);
721                                 l_res->pw =  *(l_encoding_value_ptr++);
722                                 l_res->ph =  *(l_encoding_value_ptr++);
723                                 ++l_res;
724                         }
725                         ++l_current_comp;
726                         ++l_img_comp;
727                         ++l_tccp;
728                 }
729                 // special treatment
730                 l_current_pi->include = (l_current_pi-1)->include;
731                 ++l_current_pi;
732         }
733         opj_free(l_tmp_data);
734         l_tmp_data = 00;
735         opj_free(l_tmp_ptr);
736         l_tmp_ptr = 00;
737         if
738                 (l_tcp->POC)
739         {
740                 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
741         }
742         else
743         {
744                 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
745         }
746         return l_pi;
747 }
748 
pi_update_decode_poc(opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)749 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
750 {
751         // loop
752         OPJ_UINT32 pino;
753 
754         // encoding prameters to set
755         OPJ_UINT32 l_bound;
756 
757         opj_pi_iterator_t * l_current_pi = 00;
758         opj_poc_t* l_current_poc = 0;
759 
760         // preconditions in debug
761         assert(p_pi != 00);
762         assert(p_tcp != 00);
763 
764         // initializations
765         l_bound = p_tcp->numpocs+1;
766         l_current_pi = p_pi;
767         l_current_poc = p_tcp->pocs;
768 
769         for
770                 (pino = 0;pino<l_bound;++pino)
771         {
772                 l_current_pi->poc.prg = l_current_poc->prg;
773                 l_current_pi->first = 1;
774 
775                 l_current_pi->poc.resno0 = l_current_poc->resno0;
776                 l_current_pi->poc.compno0 = l_current_poc->compno0;
777                 l_current_pi->poc.layno0 = 0;
778                 l_current_pi->poc.precno0 = 0;
779                 l_current_pi->poc.resno1 = l_current_poc->resno1;
780                 l_current_pi->poc.compno1 = l_current_poc->compno1;
781                 l_current_pi->poc.layno1 = l_current_poc->layno1;
782                 l_current_pi->poc.precno1 = p_max_precision;
783                 ++l_current_pi;
784                 ++l_current_poc;
785         }
786 }
787 
pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)788 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
789 {
790         // loop
791         OPJ_UINT32 pino;
792 
793         // encoding prameters to set
794         OPJ_UINT32 l_bound;
795 
796         opj_pi_iterator_t * l_current_pi = 00;
797         // preconditions in debug
798         assert(p_tcp != 00);
799         assert(p_pi != 00);
800 
801         // initializations
802         l_bound = p_tcp->numpocs+1;
803         l_current_pi = p_pi;
804 
805         for
806                 (pino = 0;pino<l_bound;++pino)
807         {
808                 l_current_pi->poc.prg = p_tcp->prg;
809                 l_current_pi->first = 1;
810                 l_current_pi->poc.resno0 = 0;
811                 l_current_pi->poc.compno0 = 0;
812                 l_current_pi->poc.layno0 = 0;
813                 l_current_pi->poc.precno0 = 0;
814                 l_current_pi->poc.resno1 = p_max_res;
815                 l_current_pi->poc.compno1 = l_current_pi->numcomps;
816                 l_current_pi->poc.layno1 = p_tcp->numlayers;
817                 l_current_pi->poc.precno1 = p_max_precision;
818                 ++l_current_pi;
819         }
820 }
821 
822 /**
823  * Creates a packet iterator for encoding.
824  *
825  * @param        p_image                the image being encoded.
826  * @param        p_cp                the coding parameters.
827  * @param        p_tile_no        index of the tile being encoded.
828  * @param        p_t2_mode        the type of pass for generating the packet iterator
829  * @return        a list of packet iterator that points to the first packet of the tile (not true).
830 */
pi_initialise_encode(const opj_image_t * p_image,opj_cp_t * p_cp,OPJ_UINT32 p_tile_no,J2K_T2_MODE p_t2_mode)831 opj_pi_iterator_t *pi_initialise_encode(
832                                                                                 const opj_image_t *p_image,
833                                                                                 opj_cp_t *p_cp,
834                                                                                 OPJ_UINT32 p_tile_no,
835                                                                                 J2K_T2_MODE p_t2_mode
836                                                                                 )
837 {
838         // loop
839         OPJ_UINT32 pino;
840         OPJ_UINT32 compno, resno;
841 
842         // to store w, h, dx and dy fro all components and resolutions
843         OPJ_UINT32 * l_tmp_data;
844         OPJ_UINT32 ** l_tmp_ptr;
845 
846         // encoding prameters to set
847         OPJ_UINT32 l_max_res;
848         OPJ_UINT32 l_max_prec;
849         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
850         OPJ_UINT32 l_dx_min,l_dy_min;
851         OPJ_UINT32 l_bound;
852         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
853         OPJ_UINT32 l_data_stride;
854 
855         // pointers
856         opj_pi_iterator_t *l_pi = 00;
857         opj_tcp_t *l_tcp = 00;
858         const opj_tccp_t *l_tccp = 00;
859         opj_pi_comp_t *l_current_comp = 00;
860         opj_image_comp_t * l_img_comp = 00;
861         opj_pi_iterator_t * l_current_pi = 00;
862         OPJ_UINT32 * l_encoding_value_ptr = 00;
863 
864         // preconditions in debug
865         assert(p_cp != 00);
866         assert(p_image != 00);
867         assert(p_tile_no < p_cp->tw * p_cp->th);
868 
869         // initializations
870         l_tcp = &p_cp->tcps[p_tile_no];
871         l_bound = l_tcp->numpocs+1;
872 
873         l_data_stride = 4 * J2K_MAXRLVLS;
874         l_tmp_data = (OPJ_UINT32*)opj_malloc(
875                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
876         if
877                 (! l_tmp_data)
878         {
879                 return 00;
880         }
881         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
882                 p_image->numcomps * sizeof(OPJ_UINT32 *));
883         if
884                 (! l_tmp_ptr)
885         {
886                 opj_free(l_tmp_data);
887                 return 00;
888         }
889 
890         // memory allocation for pi
891         l_pi = pi_create(p_image,p_cp,p_tile_no);
892         if
893                 (!l_pi)
894         {
895                 opj_free(l_tmp_data);
896                 opj_free(l_tmp_ptr);
897                 return 00;
898         }
899 
900         l_encoding_value_ptr = l_tmp_data;
901         // update pointer array
902         for
903                 (compno = 0; compno < p_image->numcomps; ++compno)
904         {
905                 l_tmp_ptr[compno] = l_encoding_value_ptr;
906                 l_encoding_value_ptr += l_data_stride;
907         }
908         // get encoding parameters
909         get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr);
910 
911         // step calculations
912         l_step_p = 1;
913         l_step_c = l_max_prec * l_step_p;
914         l_step_r = p_image->numcomps * l_step_c;
915         l_step_l = l_max_res * l_step_r;
916 
917         // set values for first packet iterator
918         l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
919         l_current_pi = l_pi;
920 
921         // memory allocation for include
922         l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
923         if
924                 (!l_current_pi->include)
925         {
926                 opj_free(l_tmp_data);
927                 opj_free(l_tmp_ptr);
928                 pi_destroy(l_pi, l_bound);
929                 return 00;
930         }
931         memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
932 
933         // special treatment for the first packet iterator
934         l_current_comp = l_current_pi->comps;
935         l_img_comp = p_image->comps;
936         l_tccp = l_tcp->tccps;
937         l_current_pi->tx0 = l_tx0;
938         l_current_pi->ty0 = l_ty0;
939         l_current_pi->tx1 = l_tx1;
940         l_current_pi->ty1 = l_ty1;
941         l_current_pi->dx = l_dx_min;
942         l_current_pi->dy = l_dy_min;
943         l_current_pi->step_p = l_step_p;
944         l_current_pi->step_c = l_step_c;
945         l_current_pi->step_r = l_step_r;
946         l_current_pi->step_l = l_step_l;
947 
948         /* allocation for components and number of components has already been calculated by pi_create */
949         for
950                 (compno = 0; compno < l_current_pi->numcomps; ++compno)
951         {
952                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
953                 l_encoding_value_ptr = l_tmp_ptr[compno];
954 
955                 l_current_comp->dx = l_img_comp->dx;
956                 l_current_comp->dy = l_img_comp->dy;
957                 /* resolutions have already been initialized */
958                 for
959                         (resno = 0; resno < l_current_comp->numresolutions; resno++)
960                 {
961                         l_res->pdx = *(l_encoding_value_ptr++);
962                         l_res->pdy = *(l_encoding_value_ptr++);
963                         l_res->pw =  *(l_encoding_value_ptr++);
964                         l_res->ph =  *(l_encoding_value_ptr++);
965                         ++l_res;
966                 }
967                 ++l_current_comp;
968                 ++l_img_comp;
969                 ++l_tccp;
970         }
971         ++l_current_pi;
972 
973         for
974                 (pino = 1 ; pino<l_bound ; ++pino )
975         {
976                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
977                 opj_image_comp_t * l_img_comp = p_image->comps;
978                 l_tccp = l_tcp->tccps;
979 
980                 l_current_pi->tx0 = l_tx0;
981                 l_current_pi->ty0 = l_ty0;
982                 l_current_pi->tx1 = l_tx1;
983                 l_current_pi->ty1 = l_ty1;
984                 l_current_pi->dx = l_dx_min;
985                 l_current_pi->dy = l_dy_min;
986                 l_current_pi->step_p = l_step_p;
987                 l_current_pi->step_c = l_step_c;
988                 l_current_pi->step_r = l_step_r;
989                 l_current_pi->step_l = l_step_l;
990 
991                 /* allocation for components and number of components has already been calculated by pi_create */
992                 for
993                         (compno = 0; compno < l_current_pi->numcomps; ++compno)
994                 {
995                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
996                         l_encoding_value_ptr = l_tmp_ptr[compno];
997 
998                         l_current_comp->dx = l_img_comp->dx;
999                         l_current_comp->dy = l_img_comp->dy;
1000                         /* resolutions have already been initialized */
1001                         for
1002                                 (resno = 0; resno < l_current_comp->numresolutions; resno++)
1003                         {
1004                                 l_res->pdx = *(l_encoding_value_ptr++);
1005                                 l_res->pdy = *(l_encoding_value_ptr++);
1006                                 l_res->pw =  *(l_encoding_value_ptr++);
1007                                 l_res->ph =  *(l_encoding_value_ptr++);
1008                                 ++l_res;
1009                         }
1010                         ++l_current_comp;
1011                         ++l_img_comp;
1012                         ++l_tccp;
1013                 }
1014                 // special treatment
1015                 l_current_pi->include = (l_current_pi-1)->include;
1016                 ++l_current_pi;
1017         }
1018         opj_free(l_tmp_data);
1019         l_tmp_data = 00;
1020         opj_free(l_tmp_ptr);
1021         l_tmp_ptr = 00;
1022         if
1023                 (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS))
1024         {
1025                 pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1026         }
1027         else
1028         {
1029                 pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1030         }
1031         return l_pi;
1032 }
1033 
1034 /**
1035  * Updates the encoding parameters of the codec.
1036  *
1037  * @param        p_image                the image being encoded.
1038  * @param        p_cp                the coding parameters.
1039  * @param        p_tile_no        index of the tile being encoded.
1040 */
pi_update_encoding_parameters(const opj_image_t * p_image,opj_cp_t * p_cp,OPJ_UINT32 p_tile_no)1041 void pi_update_encoding_parameters(
1042                                                                                 const opj_image_t *p_image,
1043                                                                                 opj_cp_t *p_cp,
1044                                                                                 OPJ_UINT32 p_tile_no
1045                                                                                 )
1046 {
1047         // encoding prameters to set
1048         OPJ_UINT32 l_max_res;
1049         OPJ_UINT32 l_max_prec;
1050         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1051         OPJ_UINT32 l_dx_min,l_dy_min;
1052 
1053         // pointers
1054         opj_tcp_t *l_tcp = 00;
1055 
1056         // preconditions in debug
1057         assert(p_cp != 00);
1058         assert(p_image != 00);
1059         assert(p_tile_no < p_cp->tw * p_cp->th);
1060 
1061         l_tcp = &(p_cp->tcps[p_tile_no]);
1062         // get encoding parameters
1063         get_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res);
1064         if
1065                 (l_tcp->POC)
1066         {
1067                 pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1068         }
1069         else
1070         {
1071                 pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1072         }
1073 }
1074 
1075 
1076 /**
1077  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1078  *
1079  * @param        p_image                        the image being encoded.
1080  * @param        p_cp                        the coding parameters.
1081  * @param        p_tileno                        the tile index of the tile being encoded.
1082  * @param        p_tx0                        pointer that will hold the X0 parameter for the tile
1083  * @param        p_tx1                        pointer that will hold the X1 parameter for the tile
1084  * @param        p_ty0                        pointer that will hold the Y0 parameter for the tile
1085  * @param        p_ty1                        pointer that will hold the Y1 parameter for the tile
1086  * @param        p_max_prec                pointer that will hold the the maximum precision for all the bands of the tile
1087  * @param        p_max_res                pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1088  * @param        dx_min                        pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1089  * @param        dy_min                        pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1090  */
get_encoding_parameters(const opj_image_t * p_image,const opj_cp_t * p_cp,OPJ_UINT32 p_tileno,OPJ_INT32 * p_tx0,OPJ_INT32 * p_tx1,OPJ_INT32 * p_ty0,OPJ_INT32 * p_ty1,OPJ_UINT32 * p_dx_min,OPJ_UINT32 * p_dy_min,OPJ_UINT32 * p_max_prec,OPJ_UINT32 * p_max_res)1091 void get_encoding_parameters(
1092                                                                 const opj_image_t *p_image,
1093                                                                 const opj_cp_t *p_cp,
1094                                                                 OPJ_UINT32 p_tileno,
1095                                                                 OPJ_INT32 * p_tx0,
1096                                                                 OPJ_INT32  * p_tx1,
1097                                                                 OPJ_INT32  * p_ty0,
1098                                                                 OPJ_INT32  * p_ty1,
1099                                                                 OPJ_UINT32 * p_dx_min,
1100                                                                 OPJ_UINT32 * p_dy_min,
1101                                                                 OPJ_UINT32 * p_max_prec,
1102                                                                 OPJ_UINT32 * p_max_res
1103                                                         )
1104 {
1105         // loop
1106         OPJ_UINT32  compno, resno;
1107         // pointers
1108         const opj_tcp_t *l_tcp = 00;
1109         const opj_tccp_t * l_tccp = 00;
1110         const opj_image_comp_t * l_img_comp = 00;
1111 
1112         // position in x and y of tile
1113         OPJ_UINT32 p, q;
1114 
1115         // preconditions in debug
1116         assert(p_cp != 00);
1117         assert(p_image != 00);
1118         assert(p_tileno < p_cp->tw * p_cp->th);
1119 
1120         // initializations
1121         l_tcp = &p_cp->tcps [p_tileno];
1122         l_img_comp = p_image->comps;
1123         l_tccp = l_tcp->tccps;
1124 
1125         /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
1126         p = p_tileno % p_cp->tw;
1127         q = p_tileno / p_cp->tw;
1128 
1129         // find extent of tile
1130         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1131         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1132         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1133         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1134 
1135         // max precision is 0 (can only grow)
1136         *p_max_prec = 0;
1137         *p_max_res = 0;
1138 
1139         // take the largest value for dx_min and dy_min
1140         *p_dx_min = 0x7fffffff;
1141         *p_dy_min  = 0x7fffffff;
1142 
1143         for
1144                 (compno = 0; compno < p_image->numcomps; ++compno)
1145         {
1146                 // aritmetic variables to calculate
1147                 OPJ_UINT32 l_level_no;
1148                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1149                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1150                 OPJ_UINT32 l_pdx, l_pdy;
1151                 OPJ_UINT32 l_pw, l_ph;
1152                 OPJ_UINT32 l_product;
1153                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1154 
1155                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1156                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1157                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1158                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1159                 if
1160                         (l_tccp->numresolutions > *p_max_res)
1161                 {
1162                         *p_max_res = l_tccp->numresolutions;
1163                 }
1164                 // use custom size for precincts
1165                 for
1166                         (resno = 0; resno < l_tccp->numresolutions; ++resno)
1167                 {
1168                         OPJ_UINT32 l_dx, l_dy;
1169                         // precinct width and height
1170                         l_pdx = l_tccp->prcw[resno];
1171                         l_pdy = l_tccp->prch[resno];
1172 
1173                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno));
1174                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno));
1175                         // take the minimum size for dx for each comp and resolution
1176                         *p_dx_min = uint_min(*p_dx_min, l_dx);
1177                         *p_dy_min = uint_min(*p_dy_min, l_dy);
1178                         // various calculations of extents
1179                         l_level_no = l_tccp->numresolutions - 1 - resno;
1180                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1181                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1182                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1183                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1184                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1185                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1186                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1187                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1188                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1189                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1190                         l_product = l_pw * l_ph;
1191                         // update precision
1192                         if
1193                                 (l_product > *p_max_prec)
1194                         {
1195                                 *p_max_prec = l_product;
1196                         }
1197                 }
1198                 ++l_img_comp;
1199                 ++l_tccp;
1200         }
1201 }
1202 
1203 /**
1204  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1205  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1206  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1207  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1208  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1209  *
1210  * @param        p_image                        the image being encoded.
1211  * @param        p_cp                        the coding parameters.
1212  * @param        tileno                        the tile index of the tile being encoded.
1213  * @param        p_tx0                        pointer that will hold the X0 parameter for the tile
1214  * @param        p_tx1                        pointer that will hold the X1 parameter for the tile
1215  * @param        p_ty0                        pointer that will hold the Y0 parameter for the tile
1216  * @param        p_ty1                        pointer that will hold the Y1 parameter for the tile
1217  * @param        p_max_prec                pointer that will hold the the maximum precision for all the bands of the tile
1218  * @param        p_max_res                pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1219  * @param        dx_min                        pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1220  * @param        dy_min                        pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1221  * @param        p_resolutions        pointer to an area corresponding to the one described above.
1222  */
get_all_encoding_parameters(const opj_image_t * p_image,const opj_cp_t * p_cp,OPJ_UINT32 tileno,OPJ_INT32 * p_tx0,OPJ_INT32 * p_tx1,OPJ_INT32 * p_ty0,OPJ_INT32 * p_ty1,OPJ_UINT32 * p_dx_min,OPJ_UINT32 * p_dy_min,OPJ_UINT32 * p_max_prec,OPJ_UINT32 * p_max_res,OPJ_UINT32 ** p_resolutions)1223 void get_all_encoding_parameters(
1224                                                                 const opj_image_t *p_image,
1225                                                                 const opj_cp_t *p_cp,
1226                                                                 OPJ_UINT32 tileno,
1227                                                                 OPJ_INT32 * p_tx0,
1228                                                                 OPJ_INT32 * p_tx1,
1229                                                                 OPJ_INT32 * p_ty0,
1230                                                                 OPJ_INT32 * p_ty1,
1231                                                                 OPJ_UINT32 * p_dx_min,
1232                                                                 OPJ_UINT32 * p_dy_min,
1233                                                                 OPJ_UINT32 * p_max_prec,
1234                                                                 OPJ_UINT32 * p_max_res,
1235                                                                 OPJ_UINT32 ** p_resolutions
1236                                                         )
1237 {
1238         // loop
1239         OPJ_UINT32 compno, resno;
1240 
1241         // pointers
1242         const opj_tcp_t *tcp = 00;
1243         const opj_tccp_t * l_tccp = 00;
1244         const opj_image_comp_t * l_img_comp = 00;
1245 
1246         // to store l_dx, l_dy, w and h for each resolution and component.
1247         OPJ_UINT32 * lResolutionPtr;
1248 
1249         // position in x and y of tile
1250         OPJ_UINT32 p, q;
1251 
1252         // preconditions in debug
1253         assert(p_cp != 00);
1254         assert(p_image != 00);
1255         assert(tileno < p_cp->tw * p_cp->th);
1256 
1257         // initializations
1258         tcp = &p_cp->tcps [tileno];
1259         l_tccp = tcp->tccps;
1260         l_img_comp = p_image->comps;
1261 
1262         // position in x and y of tile
1263 
1264         p = tileno % p_cp->tw;
1265         q = tileno / p_cp->tw;
1266 
1267         /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1268         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1269         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1270         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1271         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1272 
1273         // max precision and resolution is 0 (can only grow)
1274         *p_max_prec = 0;
1275         *p_max_res = 0;
1276 
1277         // take the largest value for dx_min and dy_min
1278         *p_dx_min = 0x7fffffff;
1279         *p_dy_min  = 0x7fffffff;
1280 
1281         for
1282                 (compno = 0; compno < p_image->numcomps; ++compno)
1283         {
1284                 // aritmetic variables to calculate
1285                 OPJ_UINT32 l_level_no;
1286                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1287                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1288                 OPJ_UINT32 l_product;
1289                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1290                 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1291 
1292                 lResolutionPtr = p_resolutions[compno];
1293 
1294                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1295                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1296                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1297                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1298                 if
1299                         (l_tccp->numresolutions > *p_max_res)
1300                 {
1301                         *p_max_res = l_tccp->numresolutions;
1302                 }
1303 
1304                 // use custom size for precincts
1305                 l_level_no = l_tccp->numresolutions - 1;
1306                 for
1307                         (resno = 0; resno < l_tccp->numresolutions; ++resno)
1308                 {
1309                         OPJ_UINT32 l_dx, l_dy;
1310                         // precinct width and height
1311                         l_pdx = l_tccp->prcw[resno];
1312                         l_pdy = l_tccp->prch[resno];
1313                         *lResolutionPtr++ = l_pdx;
1314                         *lResolutionPtr++ = l_pdy;
1315                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1316                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1317                         // take the minimum size for l_dx for each comp and resolution
1318                         *p_dx_min = int_min(*p_dx_min, l_dx);
1319                         *p_dy_min = int_min(*p_dy_min, l_dy);
1320                         // various calculations of extents
1321 
1322                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1323                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1324                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1325                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1326                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1327                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1328                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1329                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1330                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1331                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1332                         *lResolutionPtr++ = l_pw;
1333                         *lResolutionPtr++ = l_ph;
1334                         l_product = l_pw * l_ph;
1335                         // update precision
1336                         if
1337                                 (l_product > *p_max_prec)
1338                         {
1339                                 *p_max_prec = l_product;
1340                         }
1341                         --l_level_no;
1342                 }
1343                 ++l_tccp;
1344                 ++l_img_comp;
1345         }
1346 }
1347 
1348 /**
1349  * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
1350  * No other data is set. The include section of the packet  iterator is not allocated.
1351  *
1352  * @param        p_image                the image used to initialize the packet iterator (in fact only the number of components is relevant.
1353  * @param        p_cp                the coding parameters.
1354  * @param        p_tile_no        the index of the tile from which creating the packet iterator.
1355  */
pi_create(const opj_image_t * image,const opj_cp_t * cp,OPJ_UINT32 tileno)1356 opj_pi_iterator_t * pi_create(
1357                                                                 const opj_image_t *image,
1358                                                                 const opj_cp_t *cp,
1359                                                                 OPJ_UINT32 tileno
1360                                                         )
1361 {
1362         // loop
1363         OPJ_UINT32 pino, compno;
1364         // number of poc in the p_pi
1365         OPJ_UINT32 l_poc_bound;
1366 
1367         // pointers to tile coding parameters and components.
1368         opj_pi_iterator_t *l_pi = 00;
1369         opj_tcp_t *tcp = 00;
1370         const opj_tccp_t *tccp = 00;
1371 
1372         // current packet iterator being allocated
1373         opj_pi_iterator_t *l_current_pi = 00;
1374 
1375         // preconditions in debug
1376         assert(cp != 00);
1377         assert(image != 00);
1378         assert(tileno < cp->tw * cp->th);
1379 
1380         // initializations
1381         tcp = &cp->tcps[tileno];
1382         l_poc_bound = tcp->numpocs+1;
1383 
1384 
1385         // memory allocations
1386         l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1387 
1388         if
1389                 (!l_pi)
1390         {
1391                 return 00;
1392         }
1393         memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1394         l_current_pi = l_pi;
1395         for
1396                 (pino = 0; pino < l_poc_bound ; ++pino)
1397         {
1398                 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1399                 if
1400                         (! l_current_pi->comps)
1401                 {
1402                         pi_destroy(l_pi, l_poc_bound);
1403                         return 00;
1404                 }
1405                 l_current_pi->numcomps = image->numcomps;
1406                 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1407                 for
1408                         (compno = 0; compno < image->numcomps; ++compno)
1409                 {
1410                         opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1411                         tccp = &tcp->tccps[compno];
1412                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1413                         if
1414                                 (!comp->resolutions)
1415                         {
1416                                 pi_destroy(l_pi, l_poc_bound);
1417                                 return 00;
1418                         }
1419                         comp->numresolutions = tccp->numresolutions;
1420                         memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1421                 }
1422                 ++l_current_pi;
1423         }
1424         return l_pi;
1425 }
1426 
1427 /**
1428  * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
1429  *
1430  * @param        p_cp                the coding parameters to modify
1431  * @param        p_tileno        the tile index being concerned.
1432  * @param        p_tx0                X0 parameter for the tile
1433  * @param        p_tx1                X1 parameter for the tile
1434  * @param        p_ty0                Y0 parameter for the tile
1435  * @param        p_ty1                Y1 parameter for the tile
1436  * @param        p_max_prec        the maximum precision for all the bands of the tile
1437  * @param        p_max_res        the maximum number of resolutions for all the poc inside the tile.
1438  * @param        dx_min                the minimum dx of all the components of all the resolutions for the tile.
1439  * @param        dy_min                the minimum dy of all the components of all the resolutions for the tile.
1440  */
pi_update_encode_poc_and_final(opj_cp_t * p_cp,OPJ_UINT32 p_tileno,OPJ_INT32 p_tx0,OPJ_INT32 p_tx1,OPJ_INT32 p_ty0,OPJ_INT32 p_ty1,OPJ_UINT32 p_max_prec,OPJ_UINT32 p_max_res,OPJ_UINT32 p_dx_min,OPJ_UINT32 p_dy_min)1441 void pi_update_encode_poc_and_final (
1442                                                                          opj_cp_t *p_cp,
1443                                                                          OPJ_UINT32 p_tileno,
1444                                                                          OPJ_INT32 p_tx0,
1445                                                                          OPJ_INT32 p_tx1,
1446                                                                          OPJ_INT32 p_ty0,
1447                                                                          OPJ_INT32 p_ty1,
1448                                                                          OPJ_UINT32 p_max_prec,
1449                                                                          OPJ_UINT32 p_max_res,
1450                                      OPJ_UINT32 p_dx_min,
1451                                                                          OPJ_UINT32 p_dy_min)
1452 {
1453         // loop
1454         OPJ_UINT32 pino;
1455         // tile coding parameter
1456         opj_tcp_t *l_tcp = 00;
1457         // current poc being updated
1458         opj_poc_t * l_current_poc = 00;
1459 
1460         // number of pocs
1461         OPJ_UINT32 l_poc_bound;
1462 
1463         // preconditions in debug
1464         assert(p_cp != 00);
1465         assert(p_tileno < p_cp->tw * p_cp->th);
1466 
1467         // initializations
1468         l_tcp = &p_cp->tcps [p_tileno];
1469         /* number of iterations in the loop */
1470         l_poc_bound = l_tcp->numpocs+1;
1471 
1472         // start at first element, and to make sure the compiler will not make a calculation each time in the loop
1473         // store a pointer to the current element to modify rather than l_tcp->pocs[i]
1474         l_current_poc = l_tcp->pocs;
1475 
1476         l_current_poc->compS = l_current_poc->compno0;
1477         l_current_poc->compE = l_current_poc->compno1;
1478         l_current_poc->resS = l_current_poc->resno0;
1479         l_current_poc->resE = l_current_poc->resno1;
1480         l_current_poc->layE = l_current_poc->layno1;
1481 
1482         // special treatment for the first element
1483         l_current_poc->layS = 0;
1484         l_current_poc->prg  = l_current_poc->prg1;
1485         l_current_poc->prcS = 0;
1486 
1487         l_current_poc->prcE = p_max_prec;
1488         l_current_poc->txS = p_tx0;
1489         l_current_poc->txE = p_tx1;
1490         l_current_poc->tyS = p_ty0;
1491         l_current_poc->tyE = p_ty1;
1492         l_current_poc->dx = p_dx_min;
1493         l_current_poc->dy = p_dy_min;
1494 
1495         ++ l_current_poc;
1496         for
1497                 (pino = 1;pino < l_poc_bound ; ++pino)
1498         {
1499                 l_current_poc->compS = l_current_poc->compno0;
1500                 l_current_poc->compE= l_current_poc->compno1;
1501                 l_current_poc->resS = l_current_poc->resno0;
1502                 l_current_poc->resE = l_current_poc->resno1;
1503                 l_current_poc->layE = l_current_poc->layno1;
1504                 l_current_poc->prg  = l_current_poc->prg1;
1505                 l_current_poc->prcS = 0;
1506                 // special treatment here different from the first element
1507                 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
1508 
1509                 l_current_poc->prcE = p_max_prec;
1510                 l_current_poc->txS = p_tx0;
1511                 l_current_poc->txE = p_tx1;
1512                 l_current_poc->tyS = p_ty0;
1513                 l_current_poc->tyE = p_ty1;
1514                 l_current_poc->dx = p_dx_min;
1515                 l_current_poc->dy = p_dy_min;
1516                 ++ l_current_poc;
1517         }
1518 }
1519 
1520 /**
1521  * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
1522  *
1523  * @param        p_cp                the coding parameters to modify
1524  * @param        p_tileno        the tile index being concerned.
1525  * @param        p_tx0                X0 parameter for the tile
1526  * @param        p_tx1                X1 parameter for the tile
1527  * @param        p_ty0                Y0 parameter for the tile
1528  * @param        p_ty1                Y1 parameter for the tile
1529  * @param        p_max_prec        the maximum precision for all the bands of the tile
1530  * @param        p_max_res        the maximum number of resolutions for all the poc inside the tile.
1531  * @param        dx_min                the minimum dx of all the components of all the resolutions for the tile.
1532  * @param        dy_min                the minimum dy of all the components of all the resolutions for the tile.
1533  */
pi_update_encode_not_poc(opj_cp_t * p_cp,OPJ_UINT32 p_num_comps,OPJ_UINT32 p_tileno,OPJ_INT32 p_tx0,OPJ_INT32 p_tx1,OPJ_INT32 p_ty0,OPJ_INT32 p_ty1,OPJ_UINT32 p_max_prec,OPJ_UINT32 p_max_res,OPJ_UINT32 p_dx_min,OPJ_UINT32 p_dy_min)1534 void pi_update_encode_not_poc (
1535                                                                 opj_cp_t *p_cp,
1536                                                                 OPJ_UINT32 p_num_comps,
1537                                                                 OPJ_UINT32 p_tileno,
1538                                                                 OPJ_INT32 p_tx0,
1539                                                                 OPJ_INT32 p_tx1,
1540                                                                 OPJ_INT32 p_ty0,
1541                                                                 OPJ_INT32 p_ty1,
1542                                                                 OPJ_UINT32 p_max_prec,
1543                                                                 OPJ_UINT32 p_max_res,
1544                                 OPJ_UINT32 p_dx_min,
1545                                                                 OPJ_UINT32 p_dy_min)
1546 {
1547         // loop
1548         OPJ_UINT32 pino;
1549         // tile coding parameter
1550         opj_tcp_t *l_tcp = 00;
1551         // current poc being updated
1552         opj_poc_t * l_current_poc = 00;
1553         // number of pocs
1554         OPJ_UINT32 l_poc_bound;
1555 
1556         // preconditions in debug
1557         assert(p_cp != 00);
1558         assert(p_tileno < p_cp->tw * p_cp->th);
1559 
1560         // initializations
1561         l_tcp = &p_cp->tcps [p_tileno];
1562 
1563         /* number of iterations in the loop */
1564         l_poc_bound = l_tcp->numpocs+1;
1565 
1566         // start at first element, and to make sure the compiler will not make a calculation each time in the loop
1567         // store a pointer to the current element to modify rather than l_tcp->pocs[i]
1568         l_current_poc = l_tcp->pocs;
1569 
1570         for
1571                 (pino = 0; pino < l_poc_bound ; ++pino)
1572         {
1573                 l_current_poc->compS = 0;
1574                 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1575                 l_current_poc->resS = 0;
1576                 l_current_poc->resE = p_max_res;
1577                 l_current_poc->layS = 0;
1578                 l_current_poc->layE = l_tcp->numlayers;
1579                 l_current_poc->prg  = l_tcp->prg;
1580                 l_current_poc->prcS = 0;
1581                 l_current_poc->prcE = p_max_prec;
1582                 l_current_poc->txS = p_tx0;
1583                 l_current_poc->txE = p_tx1;
1584                 l_current_poc->tyS = p_ty0;
1585                 l_current_poc->tyE = p_ty1;
1586                 l_current_poc->dx = p_dx_min;
1587                 l_current_poc->dy = p_dy_min;
1588                 ++ l_current_poc;
1589         }
1590 }
1591 
1592 /**
1593  * Destroys a packet iterator array.
1594  *
1595  * @param        p_pi                        the packet iterator array to destroy.
1596  * @param        p_nb_elements        the number of elements in the array.
1597  */
pi_destroy(opj_pi_iterator_t * p_pi,OPJ_UINT32 p_nb_elements)1598 void pi_destroy(
1599                                 opj_pi_iterator_t *p_pi,
1600                                 OPJ_UINT32 p_nb_elements)
1601 {
1602         OPJ_UINT32 compno, pino;
1603         opj_pi_iterator_t *l_current_pi = p_pi;
1604         if
1605                 (p_pi)
1606         {
1607                 if
1608                         (p_pi->include)
1609                 {
1610                         opj_free(p_pi->include);
1611                         p_pi->include = 00;
1612                 }
1613                 // TODO
1614                 for
1615                         (pino = 0; pino < p_nb_elements; ++pino)
1616                 {
1617                         if
1618                                 (l_current_pi->comps)
1619                         {
1620                                 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1621                                 for
1622                                         (compno = 0; compno < l_current_pi->numcomps; compno++)
1623                                 {
1624                                         if
1625                                                 (l_current_component->resolutions)
1626                                         {
1627                                                 opj_free(l_current_component->resolutions);
1628                                                 l_current_component->resolutions = 00;
1629                                         }
1630                                         ++l_current_component;
1631                                 }
1632                                 opj_free(l_current_pi->comps);
1633                                 l_current_pi->comps = 0;
1634                         }
1635                         ++l_current_pi;
1636                 }
1637                 opj_free(p_pi);
1638         }
1639 }
1640 
pi_next(opj_pi_iterator_t * pi)1641 bool pi_next(opj_pi_iterator_t * pi) {
1642         switch (pi->poc.prg) {
1643                 case LRCP:
1644                         return pi_next_lrcp(pi);
1645                 case RLCP:
1646                         return pi_next_rlcp(pi);
1647                 case RPCL:
1648                         return pi_next_rpcl(pi);
1649                 case PCRL:
1650                         return pi_next_pcrl(pi);
1651                 case CPRL:
1652                         return pi_next_cprl(pi);
1653                 case PROG_UNKNOWN:
1654                         return false;
1655         }
1656 
1657         return false;
1658 }
1659 
pi_check_next_level(OPJ_INT32 pos,opj_cp_t * cp,OPJ_UINT32 tileno,OPJ_UINT32 pino,const OPJ_CHAR * prog)1660 OPJ_INT32 pi_check_next_level(OPJ_INT32 pos,opj_cp_t *cp,OPJ_UINT32 tileno, OPJ_UINT32 pino, const OPJ_CHAR *prog)
1661 {
1662         OPJ_INT32 i,l;
1663         opj_tcp_t *tcps =&cp->tcps[tileno];
1664         opj_poc_t *tcp = &tcps->pocs[pino];
1665         if(pos>=0){
1666                 for(i=pos;pos>=0;i--){
1667                         switch(prog[i]){
1668                 case 'R':
1669                         if(tcp->res_t==tcp->resE){
1670                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
1671                                 if(l==1){
1672                                         return 1;
1673                                 }else{
1674                                         return 0;
1675                                 }
1676                         }else{
1677                                 return 1;
1678                         }
1679                         break;
1680                 case 'C':
1681                         if(tcp->comp_t==tcp->compE){
1682                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
1683                                 if(l==1){
1684                                         return 1;
1685                                 }else{
1686                                         return 0;
1687                                 }
1688                         }else{
1689                                 return 1;
1690                         }
1691                         break;
1692                 case 'L':
1693                         if(tcp->lay_t==tcp->layE){
1694                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
1695                                 if(l==1){
1696                                         return 1;
1697                                 }else{
1698                                         return 0;
1699                                 }
1700                         }else{
1701                                 return 1;
1702                         }
1703                         break;
1704                 case 'P':
1705                         switch(tcp->prg){
1706                                 case LRCP||RLCP:
1707                                         if(tcp->prc_t == tcp->prcE){
1708                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1709                                                 if(l==1){
1710                                                         return 1;
1711                                                 }else{
1712                                                         return 0;
1713                                                 }
1714                                         }else{
1715                                                 return 1;
1716                                         }
1717                                         break;
1718                         default:
1719                                 if(tcp->tx0_t == tcp->txE){
1720                                         //TY
1721                                         if(tcp->ty0_t == tcp->tyE){
1722                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1723                                                 if(l==1){
1724                                                         return 1;
1725                                                 }else{
1726                                                         return 0;
1727                                                 }
1728                                         }else{
1729                                                 return 1;
1730                                         }//TY
1731                                 }else{
1732                                         return 1;
1733                                 }
1734                                 break;
1735                         }//end case P
1736                 }//end switch
1737                 }//end for
1738         }//end if
1739         return 0;
1740 }
1741 
1742 
pi_create_encode(opj_pi_iterator_t * pi,opj_cp_t * cp,OPJ_UINT32 tileno,OPJ_UINT32 pino,OPJ_UINT32 tpnum,OPJ_INT32 tppos,J2K_T2_MODE t2_mode)1743 void pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,OPJ_UINT32 tileno, OPJ_UINT32 pino,OPJ_UINT32 tpnum, OPJ_INT32 tppos, J2K_T2_MODE t2_mode)
1744 {
1745         const OPJ_CHAR *prog;
1746         OPJ_INT32 i,l;
1747         OPJ_UINT32 incr_top=1,resetX=0;
1748         opj_tcp_t *tcps =&cp->tcps[tileno];
1749         opj_poc_t *tcp= &tcps->pocs[pino];
1750 
1751         prog = j2k_convert_progression_order(tcp->prg);
1752 
1753         pi[pino].first = 1;
1754         pi[pino].poc.prg = tcp->prg;
1755 
1756         if(!(cp->m_specific_param.m_enc.m_tp_on&& ((!cp->m_specific_param.m_enc.m_cinema && (t2_mode == FINAL_PASS)) || cp->m_specific_param.m_enc.m_cinema))){
1757                 pi[pino].poc.resno0 = tcp->resS;
1758                 pi[pino].poc.resno1 = tcp->resE;
1759                 pi[pino].poc.compno0 = tcp->compS;
1760                 pi[pino].poc.compno1 = tcp->compE;
1761                 pi[pino].poc.layno0 = tcp->layS;
1762                 pi[pino].poc.layno1 = tcp->layE;
1763                 pi[pino].poc.precno0 = tcp->prcS;
1764                 pi[pino].poc.precno1 = tcp->prcE;
1765                 pi[pino].poc.tx0 = tcp->txS;
1766                 pi[pino].poc.ty0 = tcp->tyS;
1767                 pi[pino].poc.tx1 = tcp->txE;
1768                 pi[pino].poc.ty1 = tcp->tyE;
1769         }else {
1770                 for(i=tppos+1;i<4;i++){
1771                         switch(prog[i]){
1772                         case 'R':
1773                                 pi[pino].poc.resno0 = tcp->resS;
1774                                 pi[pino].poc.resno1 = tcp->resE;
1775                                 break;
1776                         case 'C':
1777                                 pi[pino].poc.compno0 = tcp->compS;
1778                                 pi[pino].poc.compno1 = tcp->compE;
1779                                 break;
1780                         case 'L':
1781                                 pi[pino].poc.layno0 = tcp->layS;
1782                                 pi[pino].poc.layno1 = tcp->layE;
1783                                 break;
1784                         case 'P':
1785                                 switch(tcp->prg){
1786                                         case LRCP:
1787                                         case RLCP:
1788                                                 pi[pino].poc.precno0 = tcp->prcS;
1789                                                 pi[pino].poc.precno1 = tcp->prcE;
1790                                                 break;
1791                                         default:
1792                                                 pi[pino].poc.tx0 = tcp->txS;
1793                                                 pi[pino].poc.ty0 = tcp->tyS;
1794                                                 pi[pino].poc.tx1 = tcp->txE;
1795                                                 pi[pino].poc.ty1 = tcp->tyE;
1796                                                 break;
1797                                 }
1798                                 break;
1799                         }
1800                 }
1801 
1802                 if(tpnum==0){
1803                         for(i=tppos;i>=0;i--){
1804                                 switch(prog[i]){
1805                                                 case 'C':
1806                                                         tcp->comp_t = tcp->compS;
1807                                                         pi[pino].poc.compno0 = tcp->comp_t;
1808                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1809                                                         tcp->comp_t+=1;
1810                                                         break;
1811                                                 case 'R':
1812                                                         tcp->res_t = tcp->resS;
1813                                                         pi[pino].poc.resno0 = tcp->res_t;
1814                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1815                                                         tcp->res_t+=1;
1816                                                         break;
1817                                                 case 'L':
1818                                                         tcp->lay_t = tcp->layS;
1819                                                         pi[pino].poc.layno0 = tcp->lay_t;
1820                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1821                                                         tcp->lay_t+=1;
1822                                                         break;
1823                                                 case 'P':
1824                                                         switch(tcp->prg){
1825                                                                 case LRCP:
1826                                                                 case RLCP:
1827                                                                         tcp->prc_t = tcp->prcS;
1828                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1829                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1830                                                                         tcp->prc_t+=1;
1831                                                                         break;
1832                                                                 default:
1833                                                                         tcp->tx0_t = tcp->txS;
1834                                                                         tcp->ty0_t = tcp->tyS;
1835                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1836                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1837                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1838                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1839                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1840                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1841                                                                         break;
1842                                                         }
1843                                                         break;
1844                                 }
1845                         }
1846                 }else{
1847                         for(i=tppos;i>=0;i--){
1848                                 switch(prog[i]){
1849                                                 case 'C':
1850                                                         pi[pino].poc.compno0 = tcp->comp_t-1;
1851                                                         pi[pino].poc.compno1 = tcp->comp_t;
1852                                                         break;
1853                                                 case 'R':
1854                                                         pi[pino].poc.resno0 = tcp->res_t-1;
1855                                                         pi[pino].poc.resno1 = tcp->res_t;
1856                                                         break;
1857                                                 case 'L':
1858                                                         pi[pino].poc.layno0 = tcp->lay_t-1;
1859                                                         pi[pino].poc.layno1 = tcp->lay_t;
1860                                                         break;
1861                                                 case 'P':
1862                                                         switch(tcp->prg){
1863                                                                 case LRCP:
1864                                                                 case RLCP:
1865                                                                         pi[pino].poc.precno0 = tcp->prc_t-1;
1866                                                                         pi[pino].poc.precno1 = tcp->prc_t;
1867                                                                         break;
1868                                                                 default:
1869                                                                         pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1870                                                                         pi[pino].poc.tx1 = tcp->tx0_t ;
1871                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1872                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
1873                                                                         break;
1874                                                         }
1875                                                         break;
1876                                 }
1877                                 if(incr_top==1){
1878                                         switch(prog[i]){
1879                                                         case 'R':
1880                                                                 if(tcp->res_t==tcp->resE){
1881                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1882                                                                         if(l==1){
1883                                                                                 tcp->res_t = tcp->resS;
1884                                                                                 pi[pino].poc.resno0 = tcp->res_t;
1885                                                                                 pi[pino].poc.resno1 = tcp->res_t+1;
1886                                                                                 tcp->res_t+=1;
1887                                                                                 incr_top=1;
1888                                                                         }else{
1889                                                                                 incr_top=0;
1890                                                                         }
1891                                                                 }else{
1892                                                                         pi[pino].poc.resno0 = tcp->res_t;
1893                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1894                                                                         tcp->res_t+=1;
1895                                                                         incr_top=0;
1896                                                                 }
1897                                                                 break;
1898                                                         case 'C':
1899                                                                 if(tcp->comp_t ==tcp->compE){
1900                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1901                                                                         if(l==1){
1902                                                                                 tcp->comp_t = tcp->compS;
1903                                                                                 pi[pino].poc.compno0 = tcp->comp_t;
1904                                                                                 pi[pino].poc.compno1 = tcp->comp_t+1;
1905                                                                                 tcp->comp_t+=1;
1906                                                                                 incr_top=1;
1907                                                                         }else{
1908                                                                                 incr_top=0;
1909                                                                         }
1910                                                                 }else{
1911                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1912                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1913                                                                         tcp->comp_t+=1;
1914                                                                         incr_top=0;
1915                                                                 }
1916                                                                 break;
1917                                                         case 'L':
1918                                                                 if(tcp->lay_t == tcp->layE){
1919                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1920                                                                         if(l==1){
1921                                                                                 tcp->lay_t = tcp->layS;
1922                                                                                 pi[pino].poc.layno0 = tcp->lay_t;
1923                                                                                 pi[pino].poc.layno1 = tcp->lay_t+1;
1924                                                                                 tcp->lay_t+=1;
1925                                                                                 incr_top=1;
1926                                                                         }else{
1927                                                                                 incr_top=0;
1928                                                                         }
1929                                                                 }else{
1930                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1931                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1932                                                                         tcp->lay_t+=1;
1933                                                                         incr_top=0;
1934                                                                 }
1935                                                                 break;
1936                                                         case 'P':
1937                                                                 switch(tcp->prg){
1938                                                                         case LRCP:
1939                                                                         case RLCP:
1940                                                                                 if(tcp->prc_t == tcp->prcE){
1941                                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1942                                                                                         if(l==1){
1943                                                                                                 tcp->prc_t = tcp->prcS;
1944                                                                                                 pi[pino].poc.precno0 = tcp->prc_t;
1945                                                                                                 pi[pino].poc.precno1 = tcp->prc_t+1;
1946                                                                                                 tcp->prc_t+=1;
1947                                                                                                 incr_top=1;
1948                                                                                         }else{
1949                                                                                                 incr_top=0;
1950                                                                                         }
1951                                                                                 }else{
1952                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1953                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1954                                                                                         tcp->prc_t+=1;
1955                                                                                         incr_top=0;
1956                                                                                 }
1957                                                                                 break;
1958                                                                         default:
1959                                                                                 if(tcp->tx0_t >= tcp->txE){
1960                                                                                         if(tcp->ty0_t >= tcp->tyE){
1961                                                                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1962                                                                                                 if(l==1){
1963                                                                                                         tcp->ty0_t = tcp->tyS;
1964                                                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1965                                                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1966                                                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1967                                                                                                         incr_top=1;resetX=1;
1968                                                                                                 }else{
1969                                                                                                         incr_top=0;resetX=0;
1970                                                                                                 }
1971                                                                                         }else{
1972                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1973                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1974                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1975                                                                                                 incr_top=0;resetX=1;
1976                                                                                         }
1977                                                                                         if(resetX==1){
1978                                                                                                 tcp->tx0_t = tcp->txS;
1979                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
1980                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1981                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
1982                                                                                         }
1983                                                                                 }else{
1984                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1985                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1986                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1987                                                                                         incr_top=0;
1988                                                                                 }
1989                                                                                 break;
1990                                                                 }
1991                                                                 break;
1992                                         }
1993                                 }
1994                         }
1995                 }
1996         }
1997 }
1998 
1999 
2000 
2001