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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
35 /** @defgroup PI PI - Implementation of a packet iterator */
38 /** @name Local static functions */
42 Get next packet in layer-resolution-component-precinct order.
43 @param pi packet iterator to modify
44 @return returns false if pi pointed to the last packet or else returns true
46 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi);
48 Get next packet in resolution-layer-component-precinct order.
49 @param pi packet iterator to modify
50 @return returns false if pi pointed to the last packet or else returns true
52 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi);
54 Get next packet in resolution-precinct-component-layer order.
55 @param pi packet iterator to modify
56 @return returns false if pi pointed to the last packet or else returns true
58 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi);
60 Get next packet in precinct-component-resolution-layer order.
61 @param pi packet iterator to modify
62 @return returns false if pi pointed to the last packet or else returns true
64 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi);
66 Get next packet in component-precinct-resolution-layer order.
67 @param pi packet iterator to modify
68 @return returns false if pi pointed to the last packet or else returns true
70 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi);
73 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
75 * @param p_cp the coding parameters to modify
76 * @param p_tileno the tile index being concerned.
77 * @param p_tx0 X0 parameter for the tile
78 * @param p_tx1 X1 parameter for the tile
79 * @param p_ty0 Y0 parameter for the tile
80 * @param p_ty1 Y1 parameter for the tile
81 * @param p_max_prec the maximum precision for all the bands of the tile
82 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
83 * @param dx_min the minimum dx of all the components of all the resolutions for the tile.
84 * @param dy_min the minimum dy of all the components of all the resolutions for the tile.
86 void pi_update_encode_poc_and_final (opj_cp_v2_t *p_cp,
92 OPJ_UINT32 p_max_prec,
98 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
100 * @param p_cp the coding parameters to modify
101 * @param p_tileno the tile index being concerned.
102 * @param p_tx0 X0 parameter for the tile
103 * @param p_tx1 X1 parameter for the tile
104 * @param p_ty0 Y0 parameter for the tile
105 * @param p_ty1 Y1 parameter for the tile
106 * @param p_max_prec the maximum precision for all the bands of the tile
107 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
108 * @param dx_min the minimum dx of all the components of all the resolutions for the tile.
109 * @param dy_min the minimum dy of all the components of all the resolutions for the tile.
111 void pi_update_encode_not_poc ( opj_cp_v2_t *p_cp,
112 OPJ_UINT32 p_num_comps,
118 OPJ_UINT32 p_max_prec,
119 OPJ_UINT32 p_max_res,
121 OPJ_UINT32 p_dy_min);
124 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
126 * @param p_image the image being encoded.
127 * @param p_cp the coding parameters.
128 * @param tileno the tile index of the tile being encoded.
129 * @param p_tx0 pointer that will hold the X0 parameter for the tile
130 * @param p_tx1 pointer that will hold the X1 parameter for the tile
131 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
132 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
133 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
134 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
135 * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
136 * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
138 void get_encoding_parameters( const opj_image_t *p_image,
139 const opj_cp_v2_t *p_cp,
145 OPJ_UINT32 * p_dx_min,
146 OPJ_UINT32 * p_dy_min,
147 OPJ_UINT32 * p_max_prec,
148 OPJ_UINT32 * p_max_res );
151 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
152 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
153 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
154 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
155 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
157 * @param p_image the image being encoded.
158 * @param p_cp the coding parameters.
159 * @param tileno the tile index of the tile being encoded.
160 * @param p_tx0 pointer that will hold the X0 parameter for the tile
161 * @param p_tx1 pointer that will hold the X1 parameter for the tile
162 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
163 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
164 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
165 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
166 * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
167 * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
168 * @param p_resolutions pointer to an area corresponding to the one described above.
170 void get_all_encoding_parameters(
171 const opj_image_t *p_image,
172 const opj_cp_v2_t *p_cp,
178 OPJ_UINT32 * p_dx_min,
179 OPJ_UINT32 * p_dy_min,
180 OPJ_UINT32 * p_max_prec,
181 OPJ_UINT32 * p_max_res,
182 OPJ_UINT32 ** p_resolutions
187 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
188 * No other data is set. The include section of the packet iterator is not allocated.
190 * @param image the image used to initialize the packet iterator (only the number of components is relevant).
191 * @param cp the coding parameters.
192 * @param tileno the index of the tile from which creating the packet iterator.
194 opj_pi_iterator_t * pi_create( const opj_image_t *image,
195 const opj_cp_v2_t *cp,
198 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
199 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
202 OPJ_INT32 pi_check_next_level( OPJ_INT32 pos,
206 const OPJ_CHAR *prog);
213 ==========================================================
215 ==========================================================
218 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) {
219 opj_pi_comp_t *comp = NULL;
220 opj_pi_resolution_t *res = NULL;
224 comp = &pi->comps[pi->compno];
225 res = &comp->resolutions[pi->resno];
231 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
232 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
234 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
235 comp = &pi->comps[pi->compno];
236 if (pi->resno >= comp->numresolutions) {
239 res = &comp->resolutions[pi->resno];
241 pi->poc.precno1 = res->pw * res->ph;
243 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
244 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
245 if (!pi->include[index]) {
246 pi->include[index] = 1;
258 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) {
259 opj_pi_comp_t *comp = NULL;
260 opj_pi_resolution_t *res = NULL;
264 comp = &pi->comps[pi->compno];
265 res = &comp->resolutions[pi->resno];
271 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
272 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
273 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
274 comp = &pi->comps[pi->compno];
275 if (pi->resno >= comp->numresolutions) {
278 res = &comp->resolutions[pi->resno];
280 pi->poc.precno1 = res->pw * res->ph;
282 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
283 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
284 if (!pi->include[index]) {
285 pi->include[index] = 1;
297 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) {
298 opj_pi_comp_t *comp = NULL;
299 opj_pi_resolution_t *res = NULL;
309 for (compno = 0; compno < pi->numcomps; compno++) {
310 comp = &pi->comps[compno];
311 for (resno = 0; resno < comp->numresolutions; resno++) {
313 res = &comp->resolutions[resno];
314 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
315 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
316 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
317 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
322 pi->poc.ty0 = pi->ty0;
323 pi->poc.tx0 = pi->tx0;
324 pi->poc.ty1 = pi->ty1;
325 pi->poc.tx1 = pi->tx1;
327 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
328 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
329 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
330 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
336 comp = &pi->comps[pi->compno];
337 if (pi->resno >= comp->numresolutions) {
340 res = &comp->resolutions[pi->resno];
341 levelno = comp->numresolutions - 1 - pi->resno;
342 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
343 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
344 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
345 try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
346 rpx = res->pdx + levelno;
347 rpy = res->pdy + levelno;
348 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
351 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
355 if ((res->pw==0)||(res->ph==0)) continue;
357 if ((trx0==trx1)||(try0==try1)) continue;
359 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
360 - int_floordivpow2(trx0, res->pdx);
361 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
362 - int_floordivpow2(try0, res->pdy);
363 pi->precno = prci + prcj * res->pw;
364 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
365 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
366 if (!pi->include[index]) {
367 pi->include[index] = 1;
380 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) {
381 opj_pi_comp_t *comp = NULL;
382 opj_pi_resolution_t *res = NULL;
386 comp = &pi->comps[pi->compno];
393 for (compno = 0; compno < pi->numcomps; compno++) {
394 comp = &pi->comps[compno];
395 for (resno = 0; resno < comp->numresolutions; resno++) {
397 res = &comp->resolutions[resno];
398 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
399 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
400 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
401 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
406 pi->poc.ty0 = pi->ty0;
407 pi->poc.tx0 = pi->tx0;
408 pi->poc.ty1 = pi->ty1;
409 pi->poc.tx1 = pi->tx1;
411 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
412 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
413 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
414 comp = &pi->comps[pi->compno];
415 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
421 res = &comp->resolutions[pi->resno];
422 levelno = comp->numresolutions - 1 - pi->resno;
423 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
424 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
425 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
426 try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
427 rpx = res->pdx + levelno;
428 rpy = res->pdy + levelno;
429 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
432 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
436 if ((res->pw==0)||(res->ph==0)) continue;
438 if ((trx0==trx1)||(try0==try1)) continue;
440 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
441 - int_floordivpow2(trx0, res->pdx);
442 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
443 - int_floordivpow2(try0, res->pdy);
444 pi->precno = prci + prcj * res->pw;
445 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
446 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
447 if (!pi->include[index]) {
448 pi->include[index] = 1;
461 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) {
462 opj_pi_comp_t *comp = NULL;
463 opj_pi_resolution_t *res = NULL;
467 comp = &pi->comps[pi->compno];
473 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
475 comp = &pi->comps[pi->compno];
478 for (resno = 0; resno < comp->numresolutions; resno++) {
480 res = &comp->resolutions[resno];
481 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
482 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
483 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
484 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
487 pi->poc.ty0 = pi->ty0;
488 pi->poc.tx0 = pi->tx0;
489 pi->poc.ty1 = pi->ty1;
490 pi->poc.tx1 = pi->tx1;
492 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
493 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
494 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
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))))){
511 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
515 if ((res->pw==0)||(res->ph==0)) continue;
517 if ((trx0==trx1)||(try0==try1)) continue;
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;
541 ==========================================================
542 Packet iterator interface
543 ==========================================================
546 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
548 int compno, resno, pino;
549 opj_pi_iterator_t *pi = NULL;
550 opj_tcp_t *tcp = NULL;
551 opj_tccp_t *tccp = NULL;
553 tcp = &cp->tcps[tileno];
555 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
557 /* TODO: throw an error */
561 for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */
567 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
568 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
569 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
570 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
571 pi[pino].numcomps = image->numcomps;
573 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
574 if(!pi[pino].comps) {
575 /* TODO: throw an error */
576 pi_destroy(pi, cp, tileno);
580 for (compno = 0; compno < pi->numcomps; compno++) {
581 int tcx0, tcy0, tcx1, tcy1;
582 opj_pi_comp_t *comp = &pi[pino].comps[compno];
583 tccp = &tcp->tccps[compno];
584 comp->dx = image->comps[compno].dx;
585 comp->dy = image->comps[compno].dy;
586 comp->numresolutions = tccp->numresolutions;
588 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
589 if(!comp->resolutions) {
590 /* TODO: throw an error */
591 pi_destroy(pi, cp, tileno);
595 tcx0 = int_ceildiv(pi->tx0, comp->dx);
596 tcy0 = int_ceildiv(pi->ty0, comp->dy);
597 tcx1 = int_ceildiv(pi->tx1, comp->dx);
598 tcy1 = int_ceildiv(pi->ty1, comp->dy);
599 if (comp->numresolutions > maxres) {
600 maxres = comp->numresolutions;
603 for (resno = 0; resno < comp->numresolutions; resno++) {
605 int rx0, ry0, rx1, ry1;
606 int px0, py0, px1, py1;
607 opj_pi_resolution_t *res = &comp->resolutions[resno];
608 if (tccp->csty & J2K_CCP_CSTY_PRT) {
609 res->pdx = tccp->prcw[resno];
610 res->pdy = tccp->prch[resno];
615 levelno = comp->numresolutions - 1 - resno;
616 rx0 = int_ceildivpow2(tcx0, levelno);
617 ry0 = int_ceildivpow2(tcy0, levelno);
618 rx1 = int_ceildivpow2(tcx1, levelno);
619 ry1 = int_ceildivpow2(tcy1, levelno);
620 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
621 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
622 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
623 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
624 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
625 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
627 if (res->pw*res->ph > maxprec) {
628 maxprec = res->pw*res->ph;
634 tccp = &tcp->tccps[0];
636 pi[pino].step_c = maxprec * pi[pino].step_p;
637 pi[pino].step_r = image->numcomps * pi[pino].step_c;
638 pi[pino].step_l = maxres * pi[pino].step_r;
641 pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
642 if(!pi[pino].include) {
643 /* TODO: throw an error */
644 pi_destroy(pi, cp, tileno);
649 pi[pino].include = pi[pino - 1].include;
654 pi[pino].poc.resno0 = 0;
655 pi[pino].poc.compno0 = 0;
656 pi[pino].poc.layno1 = tcp->numlayers;
657 pi[pino].poc.resno1 = maxres;
658 pi[pino].poc.compno1 = image->numcomps;
659 pi[pino].poc.prg = tcp->prg;
662 pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
663 pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
664 pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
665 pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
666 pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
667 pi[pino].poc.prg = tcp->pocs[pino].prg;
669 pi[pino].poc.layno0 = 0;
670 pi[pino].poc.precno0 = 0;
671 pi[pino].poc.precno1 = maxprec;
679 opj_pi_iterator_t *pi_create_decode_v2( opj_image_t *p_image,
686 OPJ_UINT32 compno, resno;
688 // to store w, h, dx and dy fro all components and resolutions
689 OPJ_UINT32 * l_tmp_data;
690 OPJ_UINT32 ** l_tmp_ptr;
692 // encoding prameters to set
693 OPJ_UINT32 l_max_res;
694 OPJ_UINT32 l_max_prec;
695 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
696 OPJ_UINT32 l_dx_min,l_dy_min;
698 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
699 OPJ_UINT32 l_data_stride;
702 opj_pi_iterator_t *l_pi = 00;
703 opj_tcp_v2_t *l_tcp = 00;
704 const opj_tccp_t *l_tccp = 00;
705 opj_pi_comp_t *l_current_comp = 00;
706 opj_image_comp_t * l_img_comp = 00;
707 opj_pi_iterator_t * l_current_pi = 00;
708 OPJ_UINT32 * l_encoding_value_ptr = 00;
710 // preconditions in debug
712 assert(p_image != 00);
713 assert(p_tile_no < p_cp->tw * p_cp->th);
716 l_tcp = &p_cp->tcps[p_tile_no];
717 l_bound = l_tcp->numpocs+1;
719 l_data_stride = 4 * J2K_MAXRLVLS;
720 l_tmp_data = (OPJ_UINT32*)opj_malloc(
721 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
727 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
728 p_image->numcomps * sizeof(OPJ_UINT32 *));
732 opj_free(l_tmp_data);
736 // memory allocation for pi
737 l_pi = pi_create(p_image, p_cp, p_tile_no);
739 opj_free(l_tmp_data);
744 l_encoding_value_ptr = l_tmp_data;
745 // update pointer array
747 (compno = 0; compno < p_image->numcomps; ++compno)
749 l_tmp_ptr[compno] = l_encoding_value_ptr;
750 l_encoding_value_ptr += l_data_stride;
752 // get encoding parameters
753 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);
757 l_step_c = l_max_prec * l_step_p;
758 l_step_r = p_image->numcomps * l_step_c;
759 l_step_l = l_max_res * l_step_r;
761 // set values for first packet iterator
764 // memory allocation for include
765 l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
767 (!l_current_pi->include)
769 opj_free(l_tmp_data);
771 pi_destroy_v2(l_pi, l_bound);
774 memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
776 // special treatment for the first packet iterator
777 l_current_comp = l_current_pi->comps;
778 l_img_comp = p_image->comps;
779 l_tccp = l_tcp->tccps;
781 l_current_pi->tx0 = l_tx0;
782 l_current_pi->ty0 = l_ty0;
783 l_current_pi->tx1 = l_tx1;
784 l_current_pi->ty1 = l_ty1;
786 //l_current_pi->dx = l_img_comp->dx;
787 //l_current_pi->dy = l_img_comp->dy;
789 l_current_pi->step_p = l_step_p;
790 l_current_pi->step_c = l_step_c;
791 l_current_pi->step_r = l_step_r;
792 l_current_pi->step_l = l_step_l;
794 /* allocation for components and number of components has already been calculated by pi_create */
796 (compno = 0; compno < l_current_pi->numcomps; ++compno)
798 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
799 l_encoding_value_ptr = l_tmp_ptr[compno];
801 l_current_comp->dx = l_img_comp->dx;
802 l_current_comp->dy = l_img_comp->dy;
803 /* resolutions have already been initialized */
805 (resno = 0; resno < l_current_comp->numresolutions; resno++)
807 l_res->pdx = *(l_encoding_value_ptr++);
808 l_res->pdy = *(l_encoding_value_ptr++);
809 l_res->pw = *(l_encoding_value_ptr++);
810 l_res->ph = *(l_encoding_value_ptr++);
820 (pino = 1 ; pino<l_bound ; ++pino )
822 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
823 opj_image_comp_t * l_img_comp = p_image->comps;
824 l_tccp = l_tcp->tccps;
826 l_current_pi->tx0 = l_tx0;
827 l_current_pi->ty0 = l_ty0;
828 l_current_pi->tx1 = l_tx1;
829 l_current_pi->ty1 = l_ty1;
830 //l_current_pi->dx = l_dx_min;
831 //l_current_pi->dy = l_dy_min;
832 l_current_pi->step_p = l_step_p;
833 l_current_pi->step_c = l_step_c;
834 l_current_pi->step_r = l_step_r;
835 l_current_pi->step_l = l_step_l;
837 /* allocation for components and number of components has already been calculated by pi_create */
839 (compno = 0; compno < l_current_pi->numcomps; ++compno)
841 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
842 l_encoding_value_ptr = l_tmp_ptr[compno];
844 l_current_comp->dx = l_img_comp->dx;
845 l_current_comp->dy = l_img_comp->dy;
846 /* resolutions have already been initialized */
848 (resno = 0; resno < l_current_comp->numresolutions; resno++)
850 l_res->pdx = *(l_encoding_value_ptr++);
851 l_res->pdy = *(l_encoding_value_ptr++);
852 l_res->pw = *(l_encoding_value_ptr++);
853 l_res->ph = *(l_encoding_value_ptr++);
861 l_current_pi->include = (l_current_pi-1)->include;
864 opj_free(l_tmp_data);
871 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
875 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
880 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){
885 opj_pi_iterator_t *pi = NULL;
886 opj_tcp_t *tcp = NULL;
887 opj_tccp_t *tccp = NULL;
889 tcp = &cp->tcps[tileno];
891 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
892 if(!pi) { return NULL;}
893 pi->tp_on = cp->tp_on;
895 for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
899 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
900 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
901 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
902 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
903 pi[pino].numcomps = image->numcomps;
905 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
906 if(!pi[pino].comps) {
907 pi_destroy(pi, cp, tileno);
911 for (compno = 0; compno < pi[pino].numcomps; compno++) {
912 int tcx0, tcy0, tcx1, tcy1;
913 opj_pi_comp_t *comp = &pi[pino].comps[compno];
914 tccp = &tcp->tccps[compno];
915 comp->dx = image->comps[compno].dx;
916 comp->dy = image->comps[compno].dy;
917 comp->numresolutions = tccp->numresolutions;
919 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
920 if(!comp->resolutions) {
921 pi_destroy(pi, cp, tileno);
925 tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
926 tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
927 tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
928 tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
929 if (comp->numresolutions > maxres) {
930 maxres = comp->numresolutions;
933 for (resno = 0; resno < comp->numresolutions; resno++) {
935 int rx0, ry0, rx1, ry1;
936 int px0, py0, px1, py1;
937 opj_pi_resolution_t *res = &comp->resolutions[resno];
938 if (tccp->csty & J2K_CCP_CSTY_PRT) {
939 res->pdx = tccp->prcw[resno];
940 res->pdy = tccp->prch[resno];
945 levelno = comp->numresolutions - 1 - resno;
946 rx0 = int_ceildivpow2(tcx0, levelno);
947 ry0 = int_ceildivpow2(tcy0, levelno);
948 rx1 = int_ceildivpow2(tcx1, levelno);
949 ry1 = int_ceildivpow2(tcy1, levelno);
950 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
951 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
952 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
953 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
954 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
955 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
957 if (res->pw*res->ph > maxprec) {
958 maxprec = res->pw * res->ph;
963 tccp = &tcp->tccps[0];
965 pi[pino].step_c = maxprec * pi[pino].step_p;
966 pi[pino].step_r = image->numcomps * pi[pino].step_c;
967 pi[pino].step_l = maxres * pi[pino].step_r;
969 for (compno = 0; compno < pi->numcomps; compno++) {
970 opj_pi_comp_t *comp = &pi->comps[compno];
971 for (resno = 0; resno < comp->numresolutions; resno++) {
973 opj_pi_resolution_t *res = &comp->resolutions[resno];
974 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
975 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
976 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
977 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
982 pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
983 if(!pi[pino].include) {
984 pi_destroy(pi, cp, tileno);
989 pi[pino].include = pi[pino - 1].include;
992 /* Generation of boundaries for each prog flag*/
993 if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
994 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
995 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
996 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
997 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
998 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
999 tcp->pocs[pino].prg = tcp->pocs[pino].prg1;
1001 tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
1003 tcp->pocs[pino].compS= 0;
1004 tcp->pocs[pino].compE= image->numcomps;
1005 tcp->pocs[pino].resS = 0;
1006 tcp->pocs[pino].resE = maxres;
1007 tcp->pocs[pino].layS = 0;
1008 tcp->pocs[pino].layE = tcp->numlayers;
1009 tcp->pocs[pino].prg = tcp->prg;
1011 tcp->pocs[pino].prcS = 0;
1012 tcp->pocs[pino].prcE = maxprec;;
1013 tcp->pocs[pino].txS = pi[pino].tx0;
1014 tcp->pocs[pino].txE = pi[pino].tx1;
1015 tcp->pocs[pino].tyS = pi[pino].ty0;
1016 tcp->pocs[pino].tyE = pi[pino].ty1;
1017 tcp->pocs[pino].dx = pi[pino].dx;
1018 tcp->pocs[pino].dy = pi[pino].dy;
1024 * Creates a packet iterator for encoding.
1026 * @param p_image the image being encoded.
1027 * @param p_cp the coding parameters.
1028 * @param p_tile_no index of the tile being encoded.
1029 * @param p_t2_mode the type of pass for generating the packet iterator
1030 * @return a list of packet iterator that points to the first packet of the tile (not true).
1032 opj_pi_iterator_t *pi_initialise_encode_v2(
1033 const opj_image_t *p_image,
1035 OPJ_UINT32 p_tile_no,
1036 J2K_T2_MODE p_t2_mode
1041 OPJ_UINT32 compno, resno;
1043 // to store w, h, dx and dy fro all components and resolutions
1044 OPJ_UINT32 * l_tmp_data;
1045 OPJ_UINT32 ** l_tmp_ptr;
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;
1053 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1054 OPJ_UINT32 l_data_stride;
1057 opj_pi_iterator_t *l_pi = 00;
1058 opj_tcp_v2_t *l_tcp = 00;
1059 const opj_tccp_t *l_tccp = 00;
1060 opj_pi_comp_t *l_current_comp = 00;
1061 opj_image_comp_t * l_img_comp = 00;
1062 opj_pi_iterator_t * l_current_pi = 00;
1063 OPJ_UINT32 * l_encoding_value_ptr = 00;
1065 // preconditions in debug
1067 assert(p_image != 00);
1068 assert(p_tile_no < p_cp->tw * p_cp->th);
1071 l_tcp = &p_cp->tcps[p_tile_no];
1072 l_bound = l_tcp->numpocs+1;
1074 l_data_stride = 4 * J2K_MAXRLVLS;
1075 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1076 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1081 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1082 p_image->numcomps * sizeof(OPJ_UINT32 *));
1084 opj_free(l_tmp_data);
1088 // memory allocation for pi
1089 l_pi = pi_create(p_image,p_cp,p_tile_no);
1091 opj_free(l_tmp_data);
1092 opj_free(l_tmp_ptr);
1096 l_encoding_value_ptr = l_tmp_data;
1097 // update pointer array
1098 for (compno = 0; compno < p_image->numcomps; ++compno) {
1099 l_tmp_ptr[compno] = l_encoding_value_ptr;
1100 l_encoding_value_ptr += l_data_stride;
1103 // get encoding parameters
1104 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);
1106 // step calculations
1108 l_step_c = l_max_prec * l_step_p;
1109 l_step_r = p_image->numcomps * l_step_c;
1110 l_step_l = l_max_res * l_step_r;
1112 // set values for first packet iterator
1113 l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
1114 l_current_pi = l_pi;
1116 // memory allocation for include
1117 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
1118 if (!l_current_pi->include) {
1119 opj_free(l_tmp_data);
1120 opj_free(l_tmp_ptr);
1121 pi_destroy_v2(l_pi, l_bound);
1124 memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
1126 // special treatment for the first packet iterator
1127 l_current_comp = l_current_pi->comps;
1128 l_img_comp = p_image->comps;
1129 l_tccp = l_tcp->tccps;
1130 l_current_pi->tx0 = l_tx0;
1131 l_current_pi->ty0 = l_ty0;
1132 l_current_pi->tx1 = l_tx1;
1133 l_current_pi->ty1 = l_ty1;
1134 l_current_pi->dx = l_dx_min;
1135 l_current_pi->dy = l_dy_min;
1136 l_current_pi->step_p = l_step_p;
1137 l_current_pi->step_c = l_step_c;
1138 l_current_pi->step_r = l_step_r;
1139 l_current_pi->step_l = l_step_l;
1141 /* allocation for components and number of components has already been calculated by pi_create */
1142 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1143 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1144 l_encoding_value_ptr = l_tmp_ptr[compno];
1146 l_current_comp->dx = l_img_comp->dx;
1147 l_current_comp->dy = l_img_comp->dy;
1149 /* resolutions have already been initialized */
1150 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1151 l_res->pdx = *(l_encoding_value_ptr++);
1152 l_res->pdy = *(l_encoding_value_ptr++);
1153 l_res->pw = *(l_encoding_value_ptr++);
1154 l_res->ph = *(l_encoding_value_ptr++);
1164 for (pino = 1 ; pino<l_bound ; ++pino ) {
1165 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
1166 opj_image_comp_t * l_img_comp = p_image->comps;
1167 l_tccp = l_tcp->tccps;
1169 l_current_pi->tx0 = l_tx0;
1170 l_current_pi->ty0 = l_ty0;
1171 l_current_pi->tx1 = l_tx1;
1172 l_current_pi->ty1 = l_ty1;
1173 l_current_pi->dx = l_dx_min;
1174 l_current_pi->dy = l_dy_min;
1175 l_current_pi->step_p = l_step_p;
1176 l_current_pi->step_c = l_step_c;
1177 l_current_pi->step_r = l_step_r;
1178 l_current_pi->step_l = l_step_l;
1180 /* allocation for components and number of components has already been calculated by pi_create */
1181 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1182 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1183 l_encoding_value_ptr = l_tmp_ptr[compno];
1185 l_current_comp->dx = l_img_comp->dx;
1186 l_current_comp->dy = l_img_comp->dy;
1187 /* resolutions have already been initialized */
1188 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1189 l_res->pdx = *(l_encoding_value_ptr++);
1190 l_res->pdy = *(l_encoding_value_ptr++);
1191 l_res->pw = *(l_encoding_value_ptr++);
1192 l_res->ph = *(l_encoding_value_ptr++);
1200 // special treatment
1201 l_current_pi->include = (l_current_pi-1)->include;
1205 opj_free(l_tmp_data);
1207 opj_free(l_tmp_ptr);
1210 if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
1211 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);
1214 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);
1221 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
1223 opj_tcp_t *tcp = &cp->tcps[tileno];
1225 for (pino = 0; pino < tcp->numpocs + 1; pino++) {
1226 if(pi[pino].comps) {
1227 for (compno = 0; compno < pi->numcomps; compno++) {
1228 opj_pi_comp_t *comp = &pi[pino].comps[compno];
1229 if(comp->resolutions) {
1230 opj_free(comp->resolutions);
1233 opj_free(pi[pino].comps);
1237 opj_free(pi->include);
1243 opj_bool pi_next(opj_pi_iterator_t * pi) {
1244 switch (pi->poc.prg) {
1246 return pi_next_lrcp(pi);
1248 return pi_next_rlcp(pi);
1250 return pi_next_rpcl(pi);
1252 return pi_next_pcrl(pi);
1254 return pi_next_cprl(pi);
1262 opj_bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos, J2K_T2_MODE t2_mode,int cur_totnum_tp){
1265 int incr_top=1,resetX=0;
1266 opj_tcp_t *tcps =&cp->tcps[tileno];
1267 opj_poc_t *tcp= &tcps->pocs[pino];
1270 pi[pino].poc.prg = tcp->prg;
1273 case CPRL: strncpy(prog, "CPRL",4);
1275 case LRCP: strncpy(prog, "LRCP",4);
1277 case PCRL: strncpy(prog, "PCRL",4);
1279 case RLCP: strncpy(prog, "RLCP",4);
1281 case RPCL: strncpy(prog, "RPCL",4);
1287 if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
1288 pi[pino].poc.resno0 = tcp->resS;
1289 pi[pino].poc.resno1 = tcp->resE;
1290 pi[pino].poc.compno0 = tcp->compS;
1291 pi[pino].poc.compno1 = tcp->compE;
1292 pi[pino].poc.layno0 = tcp->layS;
1293 pi[pino].poc.layno1 = tcp->layE;
1294 pi[pino].poc.precno0 = tcp->prcS;
1295 pi[pino].poc.precno1 = tcp->prcE;
1296 pi[pino].poc.tx0 = tcp->txS;
1297 pi[pino].poc.ty0 = tcp->tyS;
1298 pi[pino].poc.tx1 = tcp->txE;
1299 pi[pino].poc.ty1 = tcp->tyE;
1301 if( tpnum < cur_totnum_tp){
1306 pi[pino].poc.compno0 = tcp->compS;
1307 pi[pino].poc.compno1 = tcp->compE;
1310 tcp->comp_t = tcp->compS;
1311 pi[pino].poc.compno0 = tcp->comp_t;
1312 pi[pino].poc.compno1 = tcp->comp_t+1;
1316 if(tcp->comp_t ==tcp->compE){
1317 tcp->comp_t = tcp->compS;
1318 pi[pino].poc.compno0 = tcp->comp_t;
1319 pi[pino].poc.compno1 = tcp->comp_t+1;
1323 pi[pino].poc.compno0 = tcp->comp_t;
1324 pi[pino].poc.compno1 = tcp->comp_t+1;
1329 pi[pino].poc.compno0 = tcp->comp_t-1;
1330 pi[pino].poc.compno1 = tcp->comp_t;
1338 pi[pino].poc.resno0 = tcp->resS;
1339 pi[pino].poc.resno1 = tcp->resE;
1342 tcp->res_t = tcp->resS;
1343 pi[pino].poc.resno0 = tcp->res_t;
1344 pi[pino].poc.resno1 = tcp->res_t+1;
1348 if(tcp->res_t==tcp->resE){
1349 tcp->res_t = tcp->resS;
1350 pi[pino].poc.resno0 = tcp->res_t;
1351 pi[pino].poc.resno1 = tcp->res_t+1;
1355 pi[pino].poc.resno0 = tcp->res_t;
1356 pi[pino].poc.resno1 = tcp->res_t+1;
1361 pi[pino].poc.resno0 = tcp->res_t - 1;
1362 pi[pino].poc.resno1 = tcp->res_t;
1370 pi[pino].poc.layno0 = tcp->layS;
1371 pi[pino].poc.layno1 = tcp->layE;
1374 tcp->lay_t = tcp->layS;
1375 pi[pino].poc.layno0 = tcp->lay_t;
1376 pi[pino].poc.layno1 = tcp->lay_t+1;
1380 if(tcp->lay_t == tcp->layE){
1381 tcp->lay_t = tcp->layS;
1382 pi[pino].poc.layno0 = tcp->lay_t;
1383 pi[pino].poc.layno1 = tcp->lay_t+1;
1387 pi[pino].poc.layno0 = tcp->lay_t;
1388 pi[pino].poc.layno1 = tcp->lay_t+1;
1393 pi[pino].poc.layno0 = tcp->lay_t - 1;
1394 pi[pino].poc.layno1 = tcp->lay_t;
1405 pi[pino].poc.precno0 = tcp->prcS;
1406 pi[pino].poc.precno1 = tcp->prcE;
1409 tcp->prc_t = tcp->prcS;
1410 pi[pino].poc.precno0 = tcp->prc_t;
1411 pi[pino].poc.precno1 = tcp->prc_t+1;
1415 if(tcp->prc_t == tcp->prcE){
1416 tcp->prc_t = tcp->prcS;
1417 pi[pino].poc.precno0 = tcp->prc_t;
1418 pi[pino].poc.precno1 = tcp->prc_t+1;
1422 pi[pino].poc.precno0 = tcp->prc_t;
1423 pi[pino].poc.precno1 = tcp->prc_t+1;
1428 pi[pino].poc.precno0 = tcp->prc_t - 1;
1429 pi[pino].poc.precno1 = tcp->prc_t;
1436 pi[pino].poc.tx0 = tcp->txS;
1437 pi[pino].poc.ty0 = tcp->tyS;
1438 pi[pino].poc.tx1 = tcp->txE;
1439 pi[pino].poc.ty1 = tcp->tyE;
1442 tcp->tx0_t = tcp->txS;
1443 tcp->ty0_t = tcp->tyS;
1444 pi[pino].poc.tx0 = tcp->tx0_t;
1445 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1446 pi[pino].poc.ty0 = tcp->ty0_t;
1447 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1448 tcp->tx0_t = pi[pino].poc.tx1;
1449 tcp->ty0_t = pi[pino].poc.ty1;
1452 if(tcp->tx0_t >= tcp->txE){
1453 if(tcp->ty0_t >= tcp->tyE){
1454 tcp->ty0_t = tcp->tyS;
1455 pi[pino].poc.ty0 = tcp->ty0_t;
1456 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1457 tcp->ty0_t = pi[pino].poc.ty1;
1458 incr_top=1;resetX=1;
1460 pi[pino].poc.ty0 = tcp->ty0_t;
1461 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1462 tcp->ty0_t = pi[pino].poc.ty1;
1463 incr_top=0;resetX=1;
1466 tcp->tx0_t = tcp->txS;
1467 pi[pino].poc.tx0 = tcp->tx0_t;
1468 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1469 tcp->tx0_t = pi[pino].poc.tx1;
1472 pi[pino].poc.tx0 = tcp->tx0_t;
1473 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1474 tcp->tx0_t = pi[pino].poc.tx1;
1475 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1476 pi[pino].poc.ty1 = tcp->ty0_t ;
1480 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1481 pi[pino].poc.tx1 = tcp->tx0_t ;
1482 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1483 pi[pino].poc.ty1 = tcp->ty0_t ;
1498 * Updates the encoding parameters of the codec.
1500 * @param p_image the image being encoded.
1501 * @param p_cp the coding parameters.
1502 * @param p_tile_no index of the tile being encoded.
1504 void pi_update_encoding_parameters( const opj_image_t *p_image,
1506 OPJ_UINT32 p_tile_no )
1508 /* encoding parameters to set */
1509 OPJ_UINT32 l_max_res;
1510 OPJ_UINT32 l_max_prec;
1511 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1512 OPJ_UINT32 l_dx_min,l_dy_min;
1515 opj_tcp_v2_t *l_tcp = 00;
1519 assert(p_image != 00);
1520 assert(p_tile_no < p_cp->tw * p_cp->th);
1522 l_tcp = &(p_cp->tcps[p_tile_no]);
1524 /* get encoding parameters */
1525 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);
1528 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);
1531 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);
1537 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1539 * @param p_image the image being encoded.
1540 * @param p_cp the coding parameters.
1541 * @param p_tileno the tile index of the tile being encoded.
1542 * @param p_tx0 pointer that will hold the X0 parameter for the tile
1543 * @param p_tx1 pointer that will hold the X1 parameter for the tile
1544 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
1545 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
1546 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
1547 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1548 * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1549 * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1551 void get_encoding_parameters( const opj_image_t *p_image,
1552 const opj_cp_v2_t *p_cp,
1553 OPJ_UINT32 p_tileno,
1558 OPJ_UINT32 * p_dx_min,
1559 OPJ_UINT32 * p_dy_min,
1560 OPJ_UINT32 * p_max_prec,
1561 OPJ_UINT32 * p_max_res )
1564 OPJ_UINT32 compno, resno;
1566 const opj_tcp_v2_t *l_tcp = 00;
1567 const opj_tccp_t * l_tccp = 00;
1568 const opj_image_comp_t * l_img_comp = 00;
1570 /* position in x and y of tile */
1575 assert(p_image != 00);
1576 assert(p_tileno < p_cp->tw * p_cp->th);
1578 /* initializations */
1579 l_tcp = &p_cp->tcps [p_tileno];
1580 l_img_comp = p_image->comps;
1581 l_tccp = l_tcp->tccps;
1583 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
1584 p = p_tileno % p_cp->tw;
1585 q = p_tileno / p_cp->tw;
1587 /* find extent of tile */
1588 *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1589 *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1590 *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1591 *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1593 /* max precision is 0 (can only grow) */
1597 /* take the largest value for dx_min and dy_min */
1598 *p_dx_min = 0x7fffffff;
1599 *p_dy_min = 0x7fffffff;
1601 for (compno = 0; compno < p_image->numcomps; ++compno) {
1602 /* arithmetic variables to calculate */
1603 OPJ_UINT32 l_level_no;
1604 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1605 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1606 OPJ_UINT32 l_pdx, l_pdy;
1607 OPJ_UINT32 l_pw, l_ph;
1608 OPJ_UINT32 l_product;
1609 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1611 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1612 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1613 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1614 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1616 if (l_tccp->numresolutions > *p_max_res) {
1617 *p_max_res = l_tccp->numresolutions;
1620 /* use custom size for precincts */
1621 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
1622 OPJ_UINT32 l_dx, l_dy;
1624 /* precinct width and height */
1625 l_pdx = l_tccp->prcw[resno];
1626 l_pdy = l_tccp->prch[resno];
1628 l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno));
1629 l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno));
1631 /* take the minimum size for dx for each comp and resolution */
1632 *p_dx_min = uint_min(*p_dx_min, l_dx);
1633 *p_dy_min = uint_min(*p_dy_min, l_dy);
1635 /* various calculations of extents */
1636 l_level_no = l_tccp->numresolutions - 1 - resno;
1638 l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1639 l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1640 l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1641 l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1643 l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1644 l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1645 l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1647 py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1649 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1650 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1652 l_product = l_pw * l_ph;
1654 /* update precision */
1655 if (l_product > *p_max_prec) {
1656 *p_max_prec = l_product;
1665 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1666 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1667 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1668 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1669 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1671 * @param p_image the image being encoded.
1672 * @param p_cp the coding parameters.
1673 * @param tileno the tile index of the tile being encoded.
1674 * @param p_tx0 pointer that will hold the X0 parameter for the tile
1675 * @param p_tx1 pointer that will hold the X1 parameter for the tile
1676 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
1677 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
1678 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
1679 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1680 * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1681 * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1682 * @param p_resolutions pointer to an area corresponding to the one described above.
1684 void get_all_encoding_parameters(
1685 const opj_image_t *p_image,
1686 const opj_cp_v2_t *p_cp,
1692 OPJ_UINT32 * p_dx_min,
1693 OPJ_UINT32 * p_dy_min,
1694 OPJ_UINT32 * p_max_prec,
1695 OPJ_UINT32 * p_max_res,
1696 OPJ_UINT32 ** p_resolutions
1700 OPJ_UINT32 compno, resno;
1703 const opj_tcp_v2_t *tcp = 00;
1704 const opj_tccp_t * l_tccp = 00;
1705 const opj_image_comp_t * l_img_comp = 00;
1707 // to store l_dx, l_dy, w and h for each resolution and component.
1708 OPJ_UINT32 * lResolutionPtr;
1710 // position in x and y of tile
1713 // preconditions in debug
1715 assert(p_image != 00);
1716 assert(tileno < p_cp->tw * p_cp->th);
1719 tcp = &p_cp->tcps [tileno];
1720 l_tccp = tcp->tccps;
1721 l_img_comp = p_image->comps;
1723 // position in x and y of tile
1725 p = tileno % p_cp->tw;
1726 q = tileno / p_cp->tw;
1728 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1729 *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1730 *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1731 *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1732 *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1734 // max precision and resolution is 0 (can only grow)
1738 // take the largest value for dx_min and dy_min
1739 *p_dx_min = 0x7fffffff;
1740 *p_dy_min = 0x7fffffff;
1743 (compno = 0; compno < p_image->numcomps; ++compno)
1745 // aritmetic variables to calculate
1746 OPJ_UINT32 l_level_no;
1747 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1748 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1749 OPJ_UINT32 l_product;
1750 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1751 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1753 lResolutionPtr = p_resolutions[compno];
1755 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1756 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1757 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1758 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1760 (l_tccp->numresolutions > *p_max_res)
1762 *p_max_res = l_tccp->numresolutions;
1765 // use custom size for precincts
1766 l_level_no = l_tccp->numresolutions - 1;
1768 (resno = 0; resno < l_tccp->numresolutions; ++resno)
1770 OPJ_UINT32 l_dx, l_dy;
1771 // precinct width and height
1772 l_pdx = l_tccp->prcw[resno];
1773 l_pdy = l_tccp->prch[resno];
1774 *lResolutionPtr++ = l_pdx;
1775 *lResolutionPtr++ = l_pdy;
1776 l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1777 l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1778 // take the minimum size for l_dx for each comp and resolution
1779 *p_dx_min = int_min(*p_dx_min, l_dx);
1780 *p_dy_min = int_min(*p_dy_min, l_dy);
1781 // various calculations of extents
1783 l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1784 l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1785 l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1786 l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1787 l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1788 l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1789 l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1790 py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1791 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1792 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1793 *lResolutionPtr++ = l_pw;
1794 *lResolutionPtr++ = l_ph;
1795 l_product = l_pw * l_ph;
1798 (l_product > *p_max_prec)
1800 *p_max_prec = l_product;
1810 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
1811 * No other data is set. The include section of the packet iterator is not allocated.
1813 * @param image the image used to initialize the packet iterator (only the number of components is relevant.
1814 * @param cp the coding parameters.
1815 * @param tileno the index of the tile from which creating the packet iterator.
1817 opj_pi_iterator_t * pi_create( const opj_image_t *image,
1818 const opj_cp_v2_t *cp,
1822 OPJ_UINT32 pino, compno;
1823 // number of poc in the p_pi
1824 OPJ_UINT32 l_poc_bound;
1826 // pointers to tile coding parameters and components.
1827 opj_pi_iterator_t *l_pi = 00;
1828 opj_tcp_v2_t *tcp = 00;
1829 const opj_tccp_t *tccp = 00;
1831 // current packet iterator being allocated
1832 opj_pi_iterator_t *l_current_pi = 00;
1834 // preconditions in debug
1836 assert(image != 00);
1837 assert(tileno < cp->tw * cp->th);
1840 tcp = &cp->tcps[tileno];
1841 l_poc_bound = tcp->numpocs+1;
1843 // memory allocations
1844 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1848 memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1850 l_current_pi = l_pi;
1851 for (pino = 0; pino < l_poc_bound ; ++pino) {
1853 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1854 if (! l_current_pi->comps) {
1855 pi_destroy_v2(l_pi, l_poc_bound);
1859 l_current_pi->numcomps = image->numcomps;
1860 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1862 for (compno = 0; compno < image->numcomps; ++compno) {
1863 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1865 tccp = &tcp->tccps[compno];
1867 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1868 if (!comp->resolutions) {
1869 pi_destroy_v2(l_pi, l_poc_bound);
1873 comp->numresolutions = tccp->numresolutions;
1874 memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1882 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
1884 * @param p_cp the coding parameters to modify
1885 * @param p_tileno the tile index being concerned.
1886 * @param p_tx0 X0 parameter for the tile
1887 * @param p_tx1 X1 parameter for the tile
1888 * @param p_ty0 Y0 parameter for the tile
1889 * @param p_ty1 Y1 parameter for the tile
1890 * @param p_max_prec the maximum precision for all the bands of the tile
1891 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
1892 * @param dx_min the minimum dx of all the components of all the resolutions for the tile.
1893 * @param dy_min the minimum dy of all the components of all the resolutions for the tile.
1895 void pi_update_encode_poc_and_final (opj_cp_v2_t *p_cp,
1896 OPJ_UINT32 p_tileno,
1901 OPJ_UINT32 p_max_prec,
1902 OPJ_UINT32 p_max_res,
1903 OPJ_UINT32 p_dx_min,
1904 OPJ_UINT32 p_dy_min)
1908 // tile coding parameter
1909 opj_tcp_v2_t *l_tcp = 00;
1910 // current poc being updated
1911 opj_poc_t * l_current_poc = 00;
1914 OPJ_UINT32 l_poc_bound;
1916 // preconditions in debug
1918 assert(p_tileno < p_cp->tw * p_cp->th);
1921 l_tcp = &p_cp->tcps [p_tileno];
1922 /* number of iterations in the loop */
1923 l_poc_bound = l_tcp->numpocs+1;
1925 // start at first element, and to make sure the compiler will not make a calculation each time in the loop
1926 // store a pointer to the current element to modify rather than l_tcp->pocs[i]
1927 l_current_poc = l_tcp->pocs;
1929 l_current_poc->compS = l_current_poc->compno0;
1930 l_current_poc->compE = l_current_poc->compno1;
1931 l_current_poc->resS = l_current_poc->resno0;
1932 l_current_poc->resE = l_current_poc->resno1;
1933 l_current_poc->layE = l_current_poc->layno1;
1935 // special treatment for the first element
1936 l_current_poc->layS = 0;
1937 l_current_poc->prg = l_current_poc->prg1;
1938 l_current_poc->prcS = 0;
1940 l_current_poc->prcE = p_max_prec;
1941 l_current_poc->txS = p_tx0;
1942 l_current_poc->txE = p_tx1;
1943 l_current_poc->tyS = p_ty0;
1944 l_current_poc->tyE = p_ty1;
1945 l_current_poc->dx = p_dx_min;
1946 l_current_poc->dy = p_dy_min;
1949 for (pino = 1;pino < l_poc_bound ; ++pino) {
1950 l_current_poc->compS = l_current_poc->compno0;
1951 l_current_poc->compE= l_current_poc->compno1;
1952 l_current_poc->resS = l_current_poc->resno0;
1953 l_current_poc->resE = l_current_poc->resno1;
1954 l_current_poc->layE = l_current_poc->layno1;
1955 l_current_poc->prg = l_current_poc->prg1;
1956 l_current_poc->prcS = 0;
1957 // special treatment here different from the first element
1958 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
1960 l_current_poc->prcE = p_max_prec;
1961 l_current_poc->txS = p_tx0;
1962 l_current_poc->txE = p_tx1;
1963 l_current_poc->tyS = p_ty0;
1964 l_current_poc->tyE = p_ty1;
1965 l_current_poc->dx = p_dx_min;
1966 l_current_poc->dy = p_dy_min;
1972 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
1974 * @param p_cp the coding parameters to modify
1975 * @param p_tileno the tile index being concerned.
1976 * @param p_tx0 X0 parameter for the tile
1977 * @param p_tx1 X1 parameter for the tile
1978 * @param p_ty0 Y0 parameter for the tile
1979 * @param p_ty1 Y1 parameter for the tile
1980 * @param p_max_prec the maximum precision for all the bands of the tile
1981 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
1982 * @param dx_min the minimum dx of all the components of all the resolutions for the tile.
1983 * @param dy_min the minimum dy of all the components of all the resolutions for the tile.
1985 void pi_update_encode_not_poc ( opj_cp_v2_t *p_cp,
1986 OPJ_UINT32 p_num_comps,
1987 OPJ_UINT32 p_tileno,
1992 OPJ_UINT32 p_max_prec,
1993 OPJ_UINT32 p_max_res,
1994 OPJ_UINT32 p_dx_min,
1995 OPJ_UINT32 p_dy_min)
1999 // tile coding parameter
2000 opj_tcp_v2_t *l_tcp = 00;
2001 // current poc being updated
2002 opj_poc_t * l_current_poc = 00;
2004 OPJ_UINT32 l_poc_bound;
2006 // preconditions in debug
2008 assert(p_tileno < p_cp->tw * p_cp->th);
2011 l_tcp = &p_cp->tcps [p_tileno];
2013 /* number of iterations in the loop */
2014 l_poc_bound = l_tcp->numpocs+1;
2016 // start at first element, and to make sure the compiler will not make a calculation each time in the loop
2017 // store a pointer to the current element to modify rather than l_tcp->pocs[i]
2018 l_current_poc = l_tcp->pocs;
2020 for (pino = 0; pino < l_poc_bound ; ++pino) {
2021 l_current_poc->compS = 0;
2022 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
2023 l_current_poc->resS = 0;
2024 l_current_poc->resE = p_max_res;
2025 l_current_poc->layS = 0;
2026 l_current_poc->layE = l_tcp->numlayers;
2027 l_current_poc->prg = l_tcp->prg;
2028 l_current_poc->prcS = 0;
2029 l_current_poc->prcE = p_max_prec;
2030 l_current_poc->txS = p_tx0;
2031 l_current_poc->txE = p_tx1;
2032 l_current_poc->tyS = p_ty0;
2033 l_current_poc->tyE = p_ty1;
2034 l_current_poc->dx = p_dx_min;
2035 l_current_poc->dy = p_dy_min;
2041 * Destroys a packet iterator array.
2043 * @param p_pi the packet iterator array to destroy.
2044 * @param p_nb_elements the number of elements in the array.
2047 opj_pi_iterator_t *p_pi,
2048 OPJ_UINT32 p_nb_elements)
2050 OPJ_UINT32 compno, pino;
2051 opj_pi_iterator_t *l_current_pi = p_pi;
2058 opj_free(p_pi->include);
2063 (pino = 0; pino < p_nb_elements; ++pino)
2066 (l_current_pi->comps)
2068 opj_pi_comp_t *l_current_component = l_current_pi->comps;
2070 (compno = 0; compno < l_current_pi->numcomps; compno++)
2073 (l_current_component->resolutions)
2075 opj_free(l_current_component->resolutions);
2076 l_current_component->resolutions = 00;
2078 ++l_current_component;
2080 opj_free(l_current_pi->comps);
2081 l_current_pi->comps = 0;
2091 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
2096 // encoding prameters to set
2099 opj_pi_iterator_t * l_current_pi = 00;
2100 opj_poc_t* l_current_poc = 0;
2102 // preconditions in debug
2104 assert(p_tcp != 00);
2107 l_bound = p_tcp->numpocs+1;
2108 l_current_pi = p_pi;
2109 l_current_poc = p_tcp->pocs;
2112 (pino = 0;pino<l_bound;++pino)
2114 l_current_pi->poc.prg = l_current_poc->prg;
2115 l_current_pi->first = 1;
2117 l_current_pi->poc.resno0 = l_current_poc->resno0;
2118 l_current_pi->poc.compno0 = l_current_poc->compno0;
2119 l_current_pi->poc.layno0 = 0;
2120 l_current_pi->poc.precno0 = 0;
2121 l_current_pi->poc.resno1 = l_current_poc->resno1;
2122 l_current_pi->poc.compno1 = l_current_poc->compno1;
2123 l_current_pi->poc.layno1 = l_current_poc->layno1;
2124 l_current_pi->poc.precno1 = p_max_precision;
2131 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
2136 // encoding prameters to set
2139 opj_pi_iterator_t * l_current_pi = 00;
2140 // preconditions in debug
2141 assert(p_tcp != 00);
2145 l_bound = p_tcp->numpocs+1;
2146 l_current_pi = p_pi;
2149 (pino = 0;pino<l_bound;++pino)
2151 l_current_pi->poc.prg = p_tcp->prg;
2152 l_current_pi->first = 1;
2153 l_current_pi->poc.resno0 = 0;
2154 l_current_pi->poc.compno0 = 0;
2155 l_current_pi->poc.layno0 = 0;
2156 l_current_pi->poc.precno0 = 0;
2157 l_current_pi->poc.resno1 = p_max_res;
2158 l_current_pi->poc.compno1 = l_current_pi->numcomps;
2159 l_current_pi->poc.layno1 = p_tcp->numlayers;
2160 l_current_pi->poc.precno1 = p_max_precision;
2166 void pi_create_encode_v2( opj_pi_iterator_t *pi,
2172 J2K_T2_MODE t2_mode)
2174 const OPJ_CHAR *prog;
2176 OPJ_UINT32 incr_top=1,resetX=0;
2177 opj_tcp_v2_t *tcps =&cp->tcps[tileno];
2178 opj_poc_t *tcp= &tcps->pocs[pino];
2180 prog = j2k_convert_progression_order(tcp->prg);
2183 pi[pino].poc.prg = tcp->prg;
2185 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))){
2186 pi[pino].poc.resno0 = tcp->resS;
2187 pi[pino].poc.resno1 = tcp->resE;
2188 pi[pino].poc.compno0 = tcp->compS;
2189 pi[pino].poc.compno1 = tcp->compE;
2190 pi[pino].poc.layno0 = tcp->layS;
2191 pi[pino].poc.layno1 = tcp->layE;
2192 pi[pino].poc.precno0 = tcp->prcS;
2193 pi[pino].poc.precno1 = tcp->prcE;
2194 pi[pino].poc.tx0 = tcp->txS;
2195 pi[pino].poc.ty0 = tcp->tyS;
2196 pi[pino].poc.tx1 = tcp->txE;
2197 pi[pino].poc.ty1 = tcp->tyE;
2199 for(i=tppos+1;i<4;i++){
2202 pi[pino].poc.resno0 = tcp->resS;
2203 pi[pino].poc.resno1 = tcp->resE;
2206 pi[pino].poc.compno0 = tcp->compS;
2207 pi[pino].poc.compno1 = tcp->compE;
2210 pi[pino].poc.layno0 = tcp->layS;
2211 pi[pino].poc.layno1 = tcp->layE;
2217 pi[pino].poc.precno0 = tcp->prcS;
2218 pi[pino].poc.precno1 = tcp->prcE;
2221 pi[pino].poc.tx0 = tcp->txS;
2222 pi[pino].poc.ty0 = tcp->tyS;
2223 pi[pino].poc.tx1 = tcp->txE;
2224 pi[pino].poc.ty1 = tcp->tyE;
2232 for(i=tppos;i>=0;i--){
2235 tcp->comp_t = tcp->compS;
2236 pi[pino].poc.compno0 = tcp->comp_t;
2237 pi[pino].poc.compno1 = tcp->comp_t+1;
2241 tcp->res_t = tcp->resS;
2242 pi[pino].poc.resno0 = tcp->res_t;
2243 pi[pino].poc.resno1 = tcp->res_t+1;
2247 tcp->lay_t = tcp->layS;
2248 pi[pino].poc.layno0 = tcp->lay_t;
2249 pi[pino].poc.layno1 = tcp->lay_t+1;
2256 tcp->prc_t = tcp->prcS;
2257 pi[pino].poc.precno0 = tcp->prc_t;
2258 pi[pino].poc.precno1 = tcp->prc_t+1;
2262 tcp->tx0_t = tcp->txS;
2263 tcp->ty0_t = tcp->tyS;
2264 pi[pino].poc.tx0 = tcp->tx0_t;
2265 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2266 pi[pino].poc.ty0 = tcp->ty0_t;
2267 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2268 tcp->tx0_t = pi[pino].poc.tx1;
2269 tcp->ty0_t = pi[pino].poc.ty1;
2277 for(i=tppos;i>=0;i--){
2280 pi[pino].poc.compno0 = tcp->comp_t-1;
2281 pi[pino].poc.compno1 = tcp->comp_t;
2284 pi[pino].poc.resno0 = tcp->res_t-1;
2285 pi[pino].poc.resno1 = tcp->res_t;
2288 pi[pino].poc.layno0 = tcp->lay_t-1;
2289 pi[pino].poc.layno1 = tcp->lay_t;
2295 pi[pino].poc.precno0 = tcp->prc_t-1;
2296 pi[pino].poc.precno1 = tcp->prc_t;
2299 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
2300 pi[pino].poc.tx1 = tcp->tx0_t ;
2301 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
2302 pi[pino].poc.ty1 = tcp->ty0_t ;
2310 if(tcp->res_t==tcp->resE){
2311 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2313 tcp->res_t = tcp->resS;
2314 pi[pino].poc.resno0 = tcp->res_t;
2315 pi[pino].poc.resno1 = tcp->res_t+1;
2322 pi[pino].poc.resno0 = tcp->res_t;
2323 pi[pino].poc.resno1 = tcp->res_t+1;
2329 if(tcp->comp_t ==tcp->compE){
2330 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2332 tcp->comp_t = tcp->compS;
2333 pi[pino].poc.compno0 = tcp->comp_t;
2334 pi[pino].poc.compno1 = tcp->comp_t+1;
2341 pi[pino].poc.compno0 = tcp->comp_t;
2342 pi[pino].poc.compno1 = tcp->comp_t+1;
2348 if(tcp->lay_t == tcp->layE){
2349 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2351 tcp->lay_t = tcp->layS;
2352 pi[pino].poc.layno0 = tcp->lay_t;
2353 pi[pino].poc.layno1 = tcp->lay_t+1;
2360 pi[pino].poc.layno0 = tcp->lay_t;
2361 pi[pino].poc.layno1 = tcp->lay_t+1;
2370 if(tcp->prc_t == tcp->prcE){
2371 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2373 tcp->prc_t = tcp->prcS;
2374 pi[pino].poc.precno0 = tcp->prc_t;
2375 pi[pino].poc.precno1 = tcp->prc_t+1;
2382 pi[pino].poc.precno0 = tcp->prc_t;
2383 pi[pino].poc.precno1 = tcp->prc_t+1;
2389 if(tcp->tx0_t >= tcp->txE){
2390 if(tcp->ty0_t >= tcp->tyE){
2391 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2393 tcp->ty0_t = tcp->tyS;
2394 pi[pino].poc.ty0 = tcp->ty0_t;
2395 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2396 tcp->ty0_t = pi[pino].poc.ty1;
2397 incr_top=1;resetX=1;
2399 incr_top=0;resetX=0;
2402 pi[pino].poc.ty0 = tcp->ty0_t;
2403 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2404 tcp->ty0_t = pi[pino].poc.ty1;
2405 incr_top=0;resetX=1;
2408 tcp->tx0_t = tcp->txS;
2409 pi[pino].poc.tx0 = tcp->tx0_t;
2410 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
2411 tcp->tx0_t = pi[pino].poc.tx1;
2414 pi[pino].poc.tx0 = tcp->tx0_t;
2415 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
2416 tcp->tx0_t = pi[pino].poc.tx1;
2429 OPJ_INT32 pi_check_next_level( OPJ_INT32 pos,
2433 const OPJ_CHAR *prog)
2436 opj_tcp_v2_t *tcps =&cp->tcps[tileno];
2437 opj_poc_t *tcp = &tcps->pocs[pino];
2440 for(i=pos;pos>=0;i--){
2443 if(tcp->res_t==tcp->resE){
2444 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2455 if(tcp->comp_t==tcp->compE){
2456 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2467 if(tcp->lay_t==tcp->layE){
2468 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2481 if(tcp->prc_t == tcp->prcE){
2482 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2493 if(tcp->tx0_t == tcp->txE){
2495 if(tcp->ty0_t == tcp->tyE){
2496 l=pi_check_next_level(i-1,cp,tileno,pino,prog);