93a296b743abfdbbc53b73494628f0d12ac0a5d7
[openjpeg.git] / libopenjpeg / t2.c
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  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "opj_includes.h"
33
34 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
35 /*@{*/
36
37 /** @name Local static functions */
38 /*@{*/
39
40 static void t2_putcommacode(opj_bio_t *bio, int n);
41 static int t2_getcommacode(opj_bio_t *bio);
42 /**
43 Variable length code for signalling delta Zil (truncation point)
44 @param bio Bit Input/Output component
45 @param n delta Zil
46 */
47 static void t2_putnumpasses(opj_bio_t *bio, int n);
48 static int t2_getnumpasses(opj_bio_t *bio);
49 /**
50 Encode a packet of a tile to a destination buffer
51 @param tile Tile for which to write the packets
52 @param tcp Tile coding parameters
53 @param pi Packet identity
54 @param dest Destination buffer
55 @param len Length of the destination buffer
56 @param cstr_info Codestream information structure 
57 @param tileno Number of the tile encoded
58 @return 
59 */
60 static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_codestream_info_t *cstr_info, int tileno);
61 /**
62 @param cblk
63 @param index
64 @param cblksty
65 @param first
66 */
67 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first);
68 /**
69 Decode a packet of a tile from a source buffer
70 @param t2 T2 handle
71 @param src Source buffer
72 @param len Length of the source buffer
73 @param tile Tile for which to write the packets
74 @param tcp Tile coding parameters
75 @param pi Packet identity
76 @param pack_info Packet information
77 @return 
78 */
79 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, 
80                                                                                                                 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info);
81
82
83 /**
84 Decode a packet of a tile from a source buffer
85 @param t2 T2 handle
86 @param src Source buffer
87 @param len Length of the source buffer
88 @param tile Tile for which to write the packets
89 @param tcp Tile coding parameters
90 @param pi Packet identity
91 @return
92 */
93 static opj_bool t2_decode_packet_v2(
94                                                          opj_t2_v2_t* p_t2,
95                                                          opj_tcd_tile_t *p_tile,
96                              opj_tcp_v2_t *p_tcp,
97                                                          opj_pi_iterator_t *p_pi,
98                                                          OPJ_BYTE *p_src,
99                                                          OPJ_UINT32 * p_data_read,
100                                                          OPJ_UINT32 p_max_length,
101                                                          opj_packet_info_t *p_pack_info);
102
103 static opj_bool t2_skip_packet(
104                                                          opj_t2_v2_t* p_t2,
105                                                          opj_tcd_tile_t *p_tile,
106                              opj_tcp_v2_t *p_tcp,
107                                                          opj_pi_iterator_t *p_pi,
108                                                          OPJ_BYTE *p_src,
109                                                          OPJ_UINT32 * p_data_read,
110                                                          OPJ_UINT32 p_max_length,
111                                                          opj_packet_info_t *p_pack_info);
112
113 static opj_bool t2_read_packet_header(
114                                                          opj_t2_v2_t* p_t2,
115                                                          opj_tcd_tile_t *p_tile,
116                              opj_tcp_v2_t *p_tcp,
117                                                          opj_pi_iterator_t *p_pi,
118                                                          opj_bool * p_is_data_present,
119                                                          OPJ_BYTE *p_src_data,
120                                                          OPJ_UINT32 * p_data_read,
121                                                          OPJ_UINT32 p_max_length,
122                                                          opj_packet_info_t *p_pack_info);
123
124 static opj_bool t2_read_packet_data(
125                                                          opj_t2_v2_t* p_t2,
126                                                          opj_tcd_tile_t *p_tile,
127                                                          opj_pi_iterator_t *p_pi,
128                                                          OPJ_BYTE *p_src_data,
129                                                          OPJ_UINT32 * p_data_read,
130                                                          OPJ_UINT32 p_max_length,
131                                                          opj_packet_info_t *pack_info);
132
133 static opj_bool t2_skip_packet_data(
134                                                          opj_t2_v2_t* p_t2,
135                                                          opj_tcd_tile_t *p_tile,
136                                                          opj_pi_iterator_t *p_pi,
137                                                          OPJ_UINT32 * p_data_read,
138                                                          OPJ_UINT32 p_max_length,
139                                                          opj_packet_info_t *pack_info);
140
141 /**
142 @param seg
143 @param cblksty
144 @param first
145 */
146 static opj_bool t2_init_seg_v2(opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first);
147
148 /*@}*/
149
150 /*@}*/
151
152 /* ----------------------------------------------------------------------- */
153
154 /* #define RESTART 0x04 */
155
156 static void t2_putcommacode(opj_bio_t *bio, int n) {
157         while (--n >= 0) {
158                 bio_write(bio, 1, 1);
159         }
160         bio_write(bio, 0, 1);
161 }
162
163 static int t2_getcommacode(opj_bio_t *bio) {
164         int n;
165         for (n = 0; bio_read(bio, 1); n++) {
166                 ;
167         }
168         return n;
169 }
170
171 static void t2_putnumpasses(opj_bio_t *bio, int n) {
172         if (n == 1) {
173                 bio_write(bio, 0, 1);
174         } else if (n == 2) {
175                 bio_write(bio, 2, 2);
176         } else if (n <= 5) {
177                 bio_write(bio, 0xc | (n - 3), 4);
178         } else if (n <= 36) {
179                 bio_write(bio, 0x1e0 | (n - 6), 9);
180         } else if (n <= 164) {
181                 bio_write(bio, 0xff80 | (n - 37), 16);
182         }
183 }
184
185 static int t2_getnumpasses(opj_bio_t *bio) {
186         int n;
187         if (!bio_read(bio, 1))
188                 return 1;
189         if (!bio_read(bio, 1))
190                 return 2;
191         if ((n = bio_read(bio, 2)) != 3)
192                 return (3 + n);
193         if ((n = bio_read(bio, 5)) != 31)
194                 return (6 + n);
195         return (37 + bio_read(bio, 7));
196 }
197
198 static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_codestream_info_t *cstr_info, int tileno) {
199         int bandno, cblkno;
200         unsigned char *c = dest;
201
202         int compno = pi->compno;        /* component value */
203         int resno  = pi->resno;         /* resolution level value */
204         int precno = pi->precno;        /* precinct value */
205         int layno  = pi->layno;         /* quality layer value */
206
207         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
208         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
209         
210         opj_bio_t *bio = NULL;  /* BIO component */
211         
212         /* <SOP 0xff91> */
213         if (tcp->csty & J2K_CP_CSTY_SOP) {
214                 c[0] = 255;
215                 c[1] = 145;
216                 c[2] = 0;
217                 c[3] = 4;
218                 c[4] = (unsigned char)((tile->packno % 65536) / 256);
219                 c[5] = (unsigned char)((tile->packno % 65536) % 256);
220                 c += 6;
221         }
222         /* </SOP> */
223         
224         if (!layno) {
225                 for (bandno = 0; bandno < res->numbands; bandno++) {
226                         opj_tcd_band_t *band = &res->bands[bandno];
227                         opj_tcd_precinct_t *prc = &band->precincts[precno];
228                         tgt_reset(prc->incltree);
229                         tgt_reset(prc->imsbtree);
230                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
231                                 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
232                                 cblk->numpasses = 0;
233                                 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
234                         }
235                 }
236         }
237         
238         bio = bio_create();
239         bio_init_enc(bio, c, length);
240         bio_write(bio, 1, 1);           /* Empty header bit */
241         
242         /* Writing Packet header */
243         for (bandno = 0; bandno < res->numbands; bandno++) {
244                 opj_tcd_band_t *band = &res->bands[bandno];
245                 opj_tcd_precinct_t *prc = &band->precincts[precno];
246                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
247                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
248                         opj_tcd_layer_t *layer = &cblk->layers[layno];
249                         if (!cblk->numpasses && layer->numpasses) {
250                                 tgt_setvalue(prc->incltree, cblkno, layno);
251                         }
252                 }
253                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
254                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
255                         opj_tcd_layer_t *layer = &cblk->layers[layno];
256                         int increment = 0;
257                         int nump = 0;
258                         int len = 0, passno;
259                         /* cblk inclusion bits */
260                         if (!cblk->numpasses) {
261                                 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
262                         } else {
263                                 bio_write(bio, layer->numpasses != 0, 1);
264                         }
265                         /* if cblk not included, go to the next cblk  */
266                         if (!layer->numpasses) {
267                                 continue;
268                         }
269                         /* if first instance of cblk --> zero bit-planes information */
270                         if (!cblk->numpasses) {
271                                 cblk->numlenbits = 3;
272                                 tgt_encode(bio, prc->imsbtree, cblkno, 999);
273                         }
274                         /* number of coding passes included */
275                         t2_putnumpasses(bio, layer->numpasses);
276                         
277                         /* computation of the increase of the length indicator and insertion in the header     */
278                         for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
279                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
280                                 nump++;
281                                 len += pass->len;
282                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
283                                         increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
284                                         len = 0;
285                                         nump = 0;
286                                 }
287                         }
288                         t2_putcommacode(bio, increment);
289
290                         /* computation of the new Length indicator */
291                         cblk->numlenbits += increment;
292
293                         /* insertion of the codeword segment length */
294                         for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
295                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
296                                 nump++;
297                                 len += pass->len;
298                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
299                                         bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
300                                         len = 0;
301                                         nump = 0;
302                                 }
303                         }
304                 }
305         }
306
307         if (bio_flush(bio)) {
308                 bio_destroy(bio);
309                 return -999;            /* modified to eliminate longjmp !! */
310         }
311
312         c += bio_numbytes(bio);
313         bio_destroy(bio);
314         
315         /* <EPH 0xff92> */
316         if (tcp->csty & J2K_CP_CSTY_EPH) {
317                 c[0] = 255;
318                 c[1] = 146;
319                 c += 2;
320         }
321         /* </EPH> */
322
323         /* << INDEX */
324         // End of packet header position. Currently only represents the distance to start of packet
325         // Will be updated later by incrementing with packet start value
326         if(cstr_info && cstr_info->index_write) {
327                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
328                 info_PK->end_ph_pos = (int)(c - dest);
329         }
330         /* INDEX >> */
331         
332         /* Writing the packet body */
333         
334         for (bandno = 0; bandno < res->numbands; bandno++) {
335                 opj_tcd_band_t *band = &res->bands[bandno];
336                 opj_tcd_precinct_t *prc = &band->precincts[precno];
337                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
338                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
339                         opj_tcd_layer_t *layer = &cblk->layers[layno];
340                         if (!layer->numpasses) {
341                                 continue;
342                         }
343                         if (c + layer->len > dest + length) {
344                                 return -999;
345                         }
346                         
347                         memcpy(c, layer->data, layer->len);
348                         cblk->numpasses += layer->numpasses;
349                         c += layer->len;
350                         /* << INDEX */ 
351                         if(cstr_info && cstr_info->index_write) {
352                                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
353                                 info_PK->disto += layer->disto;
354                                 if (cstr_info->D_max < info_PK->disto) {
355                                         cstr_info->D_max = info_PK->disto;
356                                 }
357                         }
358                         /* INDEX >> */
359                 }
360         }
361         
362         return (c - dest);
363 }
364
365 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) {
366         opj_tcd_seg_t* seg;
367         cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
368         seg = &cblk->segs[index];
369         seg->data = NULL;
370         seg->dataindex = 0;
371         seg->numpasses = 0;
372         seg->len = 0;
373         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
374                 seg->maxpasses = 1;
375         }
376         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
377                 if (first) {
378                         seg->maxpasses = 10;
379                 } else {
380                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
381                 }
382         } else {
383                 seg->maxpasses = 109;
384         }
385 }
386
387 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, 
388                                                                                                                 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
389         int bandno, cblkno;
390         unsigned char *c = src;
391
392         opj_cp_t *cp = t2->cp;
393
394         int compno = pi->compno;        /* component value */
395         int resno  = pi->resno;         /* resolution level value */
396         int precno = pi->precno;        /* precinct value */
397         int layno  = pi->layno;         /* quality layer value */
398
399         opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno];
400
401         unsigned char *hd = NULL;
402         int present;
403         
404         opj_bio_t *bio = NULL;  /* BIO component */
405         
406         if (layno == 0) {
407                 for (bandno = 0; bandno < res->numbands; bandno++) {
408                         opj_tcd_band_t *band = &res->bands[bandno];
409                         opj_tcd_precinct_t *prc = &band->precincts[precno];
410                         
411                         if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
412                         
413                         tgt_reset(prc->incltree);
414                         tgt_reset(prc->imsbtree);
415                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
416                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
417                                 cblk->numsegs = 0;
418                         }
419                 }
420         }
421         
422         /* SOP markers */
423         
424         if (tcp->csty & J2K_CP_CSTY_SOP) {
425                 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
426                         opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
427                 } else {
428                         c += 6;
429                 }
430                 
431                 /** TODO : check the Nsop value */
432         }
433         
434         /* 
435         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
436         This part deal with this caracteristic
437         step 1: Read packet header in the saved structure
438         step 2: Return to codestream for decoding 
439         */
440
441         bio = bio_create();
442         
443         if (cp->ppm == 1) {             /* PPM */
444                 hd = cp->ppm_data;
445                 bio_init_dec(bio, hd, cp->ppm_len);
446         } else if (tcp->ppt == 1) {     /* PPT */
447                 hd = tcp->ppt_data;
448                 bio_init_dec(bio, hd, tcp->ppt_len);
449         } else {                        /* Normal Case */
450                 hd = c;
451                 bio_init_dec(bio, hd, src+len-hd);
452         }
453         
454         present = bio_read(bio, 1);
455         
456         if (!present) {
457                 bio_inalign(bio);
458                 hd += bio_numbytes(bio);
459                 bio_destroy(bio);
460                 
461                 /* EPH markers */
462                 
463                 if (tcp->csty & J2K_CP_CSTY_EPH) {
464                         if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
465                                 printf("Error : expected EPH marker\n");
466                         } else {
467                                 hd += 2;
468                         }
469                 }
470
471                 /* << INDEX */
472                 // End of packet header position. Currently only represents the distance to start of packet
473                 // Will be updated later by incrementing with packet start value
474                 if(pack_info) {
475                         pack_info->end_ph_pos = (int)(c - src);
476                 }
477                 /* INDEX >> */
478                 
479                 if (cp->ppm == 1) {             /* PPM case */
480                         cp->ppm_len += cp->ppm_data-hd;
481                         cp->ppm_data = hd;
482                         return (c - src);
483                 }
484                 if (tcp->ppt == 1) {    /* PPT case */
485                         tcp->ppt_len+=tcp->ppt_data-hd;
486                         tcp->ppt_data = hd;
487                         return (c - src);
488                 }
489                 
490                 return (hd - src);
491         }
492         
493         for (bandno = 0; bandno < res->numbands; bandno++) {
494                 opj_tcd_band_t *band = &res->bands[bandno];
495                 opj_tcd_precinct_t *prc = &band->precincts[precno];
496                 
497                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
498                 
499                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
500                         int included, increment, n, segno;
501                         opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
502                         /* if cblk not yet included before --> inclusion tagtree */
503                         if (!cblk->numsegs) {
504                                 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
505                                 /* else one bit */
506                         } else {
507                                 included = bio_read(bio, 1);
508                         }
509                         /* if cblk not included */
510                         if (!included) {
511                                 cblk->numnewpasses = 0;
512                                 continue;
513                         }
514                         /* if cblk not yet included --> zero-bitplane tagtree */
515                         if (!cblk->numsegs) {
516                                 int i, numimsbs;
517                                 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
518                                         ;
519                                 }
520                                 numimsbs = i - 1;
521                                 cblk->numbps = band->numbps - numimsbs;
522                                 cblk->numlenbits = 3;
523                         }
524                         /* number of coding passes */
525                         cblk->numnewpasses = t2_getnumpasses(bio);
526                         increment = t2_getcommacode(bio);
527                         /* length indicator increment */
528                         cblk->numlenbits += increment;
529                         segno = 0;
530                         if (!cblk->numsegs) {
531                                 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1);
532                         } else {
533                                 segno = cblk->numsegs - 1;
534                                 if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
535                                         ++segno;
536                                         t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
537                                 }
538                         }
539                         n = cblk->numnewpasses;
540                         
541                         do {
542                                 cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
543                                 cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
544                                 n -= cblk->segs[segno].numnewpasses;
545                                 if (n > 0) {
546                                         ++segno;
547                                         t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
548                                 }
549                         } while (n > 0);
550                 }
551         }
552         
553         if (bio_inalign(bio)) {
554                 bio_destroy(bio);
555                 return -999;
556         }
557         
558         hd += bio_numbytes(bio);
559         bio_destroy(bio);
560         
561         /* EPH markers */
562         if (tcp->csty & J2K_CP_CSTY_EPH) {
563                 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
564                         opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
565                         return -999;
566                 } else {
567                         hd += 2;
568                 }
569         }
570
571         /* << INDEX */
572         // End of packet header position. Currently only represents the distance to start of packet
573         // Will be updated later by incrementing with packet start value
574         if(pack_info) {
575                 pack_info->end_ph_pos = (int)(hd - src);
576         }
577         /* INDEX >> */
578         
579         if (cp->ppm==1) {
580                 cp->ppm_len+=cp->ppm_data-hd;
581                 cp->ppm_data = hd;
582         } else if (tcp->ppt == 1) {
583                 tcp->ppt_len+=tcp->ppt_data-hd;
584                 tcp->ppt_data = hd;
585         } else {
586                 c=hd;
587         }
588         
589         for (bandno = 0; bandno < res->numbands; bandno++) {
590                 opj_tcd_band_t *band = &res->bands[bandno];
591                 opj_tcd_precinct_t *prc = &band->precincts[precno];
592                 
593                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
594                 
595                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
596                         opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
597                         opj_tcd_seg_t *seg = NULL;
598                         if (!cblk->numnewpasses)
599                                 continue;
600                         if (!cblk->numsegs) {
601                                 seg = &cblk->segs[0];
602                                 cblk->numsegs++;
603                                 cblk->len = 0;
604                         } else {
605                                 seg = &cblk->segs[cblk->numsegs - 1];
606                                 if (seg->numpasses == seg->maxpasses) {
607                                         seg++;
608                                         cblk->numsegs++;
609                                 }
610                         }
611                         
612                         do {
613                                 if (c + seg->newlen > src + len) {
614                                         return -999;
615                                 }
616
617 #ifdef USE_JPWL
618                         /* we need here a j2k handle to verify if making a check to
619                         the validity of cblocks parameters is selected from user (-W) */
620
621                                 /* let's check that we are not exceeding */
622                                 if ((cblk->len + seg->newlen) > 8192) {
623                                         opj_event_msg(t2->cinfo, EVT_WARNING,
624                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
625                                                 seg->newlen, cblkno, precno, bandno, resno, compno);
626                                         if (!JPWL_ASSUME) {
627                                                 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
628                                                 return -999;
629                                         }
630                                         seg->newlen = 8192 - cblk->len;
631                                         opj_event_msg(t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", seg->newlen);
632                                         break;
633                                 };
634
635 #endif /* USE_JPWL */
636                                 
637                                 cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char*));
638                                 memcpy(cblk->data + cblk->len, c, seg->newlen);
639                                 if (seg->numpasses == 0) {
640                                         seg->data = &cblk->data;
641                                         seg->dataindex = cblk->len;
642                                 }
643                                 c += seg->newlen;
644                                 cblk->len += seg->newlen;
645                                 seg->len += seg->newlen;
646                                 seg->numpasses += seg->numnewpasses;
647                                 cblk->numnewpasses -= seg->numnewpasses;
648                                 if (cblk->numnewpasses > 0) {
649                                         seg++;
650                                         cblk->numsegs++;
651                                 }
652                         } while (cblk->numnewpasses > 0);
653                 }
654         }
655         
656         return (c - src);
657 }
658
659 /* ----------------------------------------------------------------------- */
660
661 int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode, int cur_totnum_tp){
662         unsigned char *c = dest;
663         int e = 0;
664         int compno;
665         opj_pi_iterator_t *pi = NULL;
666         int poc;
667         opj_image_t *image = t2->image;
668         opj_cp_t *cp = t2->cp;
669         opj_tcp_t *tcp = &cp->tcps[tileno];
670         int pocno = cp->cinema == CINEMA4K_24? 2: 1;
671         int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
672         
673         pi = pi_initialise_encode(image, cp, tileno, t2_mode);
674         if(!pi) {
675                 /* TODO: throw an error */
676                 return -999;
677         }
678         
679         if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
680                 for(compno = 0; compno < maxcomp; compno++ ){
681                         for(poc = 0; poc < pocno ; poc++){
682                                 int comp_len = 0;
683                                 int tpnum = compno;
684                                 if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
685                                         opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
686                                         pi_destroy(pi, cp, tileno);
687                                         return -999;
688                                 }
689                                 while (pi_next(&pi[poc])) {
690                                         if (pi[poc].layno < maxlayers) {
691                                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
692                                                 comp_len = comp_len + e;
693                                                 if (e == -999) {
694                                                         break;
695                                                 } else {
696                                                         c += e;
697                                                 }
698                                         }
699                                 }
700                                 if (e == -999) break;
701                                 if (cp->max_comp_size){
702                                         if (comp_len > cp->max_comp_size){
703                                                 e = -999;
704                                                 break;
705                                         }
706                                 }
707                         }
708                         if (e == -999)  break;
709                 }
710         }else{  /* t2_mode == FINAL_PASS  */
711                 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
712                 while (pi_next(&pi[pino])) {
713                         if (pi[pino].layno < maxlayers) {
714                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
715                                 if (e == -999) {
716                                         break;
717                                 } else {
718                                         c += e;
719                                 }
720                                 /* INDEX >> */
721                                 if(cstr_info) {
722                                         if(cstr_info->index_write) {
723                                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
724                                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
725                                                 if (!cstr_info->packno) {
726                                                         info_PK->start_pos = info_TL->end_header + 1;
727                                                 } else {
728                                                         info_PK->start_pos = ((cp->tp_on | tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
729                                                 }
730                                                 info_PK->end_pos = info_PK->start_pos + e - 1;
731                                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance 
732                                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet
733                                         }
734                                         
735                                         cstr_info->packno++;
736                                 }
737                                 /* << INDEX */
738                                 tile->packno++;
739                         }
740                 }
741         }
742         
743         pi_destroy(pi, cp, tileno);
744         
745         if (e == -999) {
746                 return e;
747         }
748         
749   return (c - dest);
750 }
751
752 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile, opj_codestream_info_t *cstr_info) {
753         unsigned char *c = src;
754         opj_pi_iterator_t *pi;
755         int pino, e = 0;
756         int n = 0, curtp = 0;
757         int tp_start_packno;
758
759         opj_image_t *image = t2->image;
760         opj_cp_t *cp = t2->cp;
761         
762         /* create a packet iterator */
763         pi = pi_create_decode(image, cp, tileno);
764         if(!pi) {
765                 /* TODO: throw an error */
766                 return -999;
767         }
768
769         tp_start_packno = 0;
770         
771         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
772                 while (pi_next(&pi[pino])) {
773                         if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
774                                 opj_packet_info_t *pack_info;
775                                 if (cstr_info)
776                                         pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
777                                 else
778                                         pack_info = NULL;
779                                 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
780                         } else {
781                                 e = 0;
782                         }
783                         if(e == -999) return -999;
784                         /* progression in resolution */
785                         image->comps[pi[pino].compno].resno_decoded =   
786                                 (e > 0) ? 
787                                 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
788                                 : image->comps[pi[pino].compno].resno_decoded;
789                         n++;
790
791                         /* INDEX >> */
792                         if(cstr_info) {
793                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
794                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
795                                 if (!cstr_info->packno) {
796                                         info_PK->start_pos = info_TL->end_header + 1;
797                                 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ // New tile part
798                                         info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
799           info_TL->tp[curtp].tp_start_pack = tp_start_packno;
800                                         tp_start_packno = cstr_info->packno;
801                                         curtp++;
802                                         info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
803                                 } else {
804                                         info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
805                                 }
806                                 info_PK->end_pos = info_PK->start_pos + e - 1;
807                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance 
808                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet
809                                 cstr_info->packno++;
810                         }
811                         /* << INDEX */
812                         
813                         if (e == -999) {                /* ADD */
814                                 break;
815                         } else {
816                                 c += e;
817                         }                       
818                 }
819         }
820         /* INDEX >> */
821         if(cstr_info) {
822                 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in last tile-part
823     cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno;
824         }
825         /* << INDEX */
826
827         /* don't forget to release pi */
828         pi_destroy(pi, cp, tileno);
829         
830         if (e == -999) {
831                 return e;
832         }
833         
834         return (c - src);
835 }
836
837 opj_bool t2_decode_packets_v2(
838                                                 opj_t2_v2_t *p_t2,
839                                                 OPJ_UINT32 p_tile_no,
840                                                 struct opj_tcd_tile *p_tile,
841                                                 OPJ_BYTE *p_src,
842                                                 OPJ_UINT32 * p_data_read,
843                                                 OPJ_UINT32 p_max_len,
844                                                 struct opj_codestream_info *p_cstr_info)
845 {
846         OPJ_BYTE *l_current_data = p_src;
847         opj_pi_iterator_t *l_pi = 00;
848         OPJ_UINT32 pino;
849         opj_image_t *l_image = p_t2->image;
850         opj_cp_v2_t *l_cp = p_t2->cp;
851         opj_cp_v2_t *cp = p_t2->cp;
852         opj_tcp_v2_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
853         OPJ_UINT32 l_nb_bytes_read;
854         OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
855         opj_pi_iterator_t *l_current_pi = 00;
856         OPJ_UINT32 curtp = 0;
857         OPJ_UINT32 tp_start_packno;
858         opj_packet_info_t *l_pack_info = 00;
859         opj_image_comp_t* l_img_comp = 00;
860
861
862         if
863                 (p_cstr_info)
864         {
865                 l_pack_info = p_cstr_info->tile[p_tile_no].packet;
866         }
867
868         /* create a packet iterator */
869         l_pi = pi_create_decode_v2(l_image, l_cp, p_tile_no);
870         if
871                 (!l_pi)
872         {
873                 return OPJ_FALSE;
874         }
875
876         tp_start_packno = 0;
877         l_current_pi = l_pi;
878
879         for
880                 (pino = 0; pino <= l_tcp->numpocs; ++pino)
881         {
882                 while
883                         (pi_next(l_current_pi))
884                 {
885
886                         if
887                                 (l_tcp->num_layers_to_decode > l_current_pi->layno && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions)
888                         {
889                                 l_nb_bytes_read = 0;
890                                 if
891                                         (! t2_decode_packet_v2(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info))
892                                 {
893                                         pi_destroy_v2(l_pi,l_nb_pocs);
894                                         return OPJ_FALSE;
895                                 }
896                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
897                                 l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
898                         }
899                         else
900                         {
901                                 l_nb_bytes_read = 0;
902                                 if
903                                         (! t2_skip_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info))
904                                 {
905                                         pi_destroy_v2(l_pi,l_nb_pocs);
906                                         return OPJ_FALSE;
907                                 }
908                         }
909                         l_current_data += l_nb_bytes_read;
910                         p_max_len -= l_nb_bytes_read;
911
912                         /* INDEX >> */
913                         if(p_cstr_info) {
914                                 opj_tile_info_t *info_TL = &p_cstr_info->tile[p_tile_no];
915                                 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
916                                 if (!p_cstr_info->packno) {
917                                         info_PK->start_pos = info_TL->end_header + 1;
918                                 } else if (info_TL->packet[p_cstr_info->packno-1].end_pos >= (OPJ_INT32)p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos){ // New tile part
919                                         info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
920                                         tp_start_packno = p_cstr_info->packno;
921                                         curtp++;
922                                         info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
923                                 } else {
924                                         info_PK->start_pos = (cp->m_specific_param.m_enc.m_tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - 1].end_pos + 1;
925                                 }
926                                 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
927                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance
928                                 ++p_cstr_info->packno;
929                         }
930                         /* << INDEX */
931                 }
932                 ++l_current_pi;
933         }
934         /* INDEX >> */
935         if
936                 (p_cstr_info) {
937                 p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; // Number of packets in last tile-part
938         }
939         /* << INDEX */
940
941         /* don't forget to release pi */
942         pi_destroy_v2(l_pi,l_nb_pocs);
943         *p_data_read = l_current_data - p_src;
944         return OPJ_TRUE;
945 }
946
947 /* ----------------------------------------------------------------------- */
948
949 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
950         /* create the tcd structure */
951         opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
952         if(!t2) return NULL;
953         t2->cinfo = cinfo;
954         t2->image = image;
955         t2->cp = cp;
956
957         return t2;
958 }
959
960 /**
961  * Creates a Tier 2 handle
962  *
963  * @param       p_image         Source or destination image
964  * @param       p_cp            Image coding parameters.
965  * @return              a new T2 handle if successful, NULL otherwise.
966 */
967 opj_t2_v2_t* t2_create_v2(
968                                         opj_image_t *p_image,
969                                         opj_cp_v2_t *p_cp)
970 {
971         /* create the tcd structure */
972         opj_t2_v2_t *l_t2 = (opj_t2_v2_t*)opj_malloc(sizeof(opj_t2_v2_t));
973         if
974                 (!l_t2)
975         {
976                 return 00;
977         }
978         memset(l_t2,0,sizeof(opj_t2_t));
979         l_t2->image = p_image;
980         l_t2->cp = p_cp;
981         return l_t2;
982 }
983
984 void t2_destroy(opj_t2_t *t2) {
985         if(t2) {
986                 opj_free(t2);
987         }
988 }
989
990 void t2_destroy_v2(opj_t2_v2_t *t2) {
991         if(t2) {
992                 opj_free(t2);
993         }
994 }
995
996
997 static opj_bool t2_decode_packet_v2(
998                                                          opj_t2_v2_t* p_t2,
999                                                          opj_tcd_tile_t *p_tile,
1000                              opj_tcp_v2_t *p_tcp,
1001                                                          opj_pi_iterator_t *p_pi,
1002                                                          OPJ_BYTE *p_src,
1003                                                          OPJ_UINT32 * p_data_read,
1004                                                          OPJ_UINT32 p_max_length,
1005                                                          opj_packet_info_t *p_pack_info)
1006 {
1007         opj_bool l_read_data;
1008         OPJ_UINT32 l_nb_bytes_read = 0;
1009         OPJ_UINT32 l_nb_total_bytes_read = 0;
1010
1011         *p_data_read = 0;
1012
1013         if
1014                 (! t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info))
1015         {
1016                 return OPJ_FALSE;
1017         }
1018         p_src += l_nb_bytes_read;
1019         l_nb_total_bytes_read += l_nb_bytes_read;
1020         p_max_length -= l_nb_bytes_read;
1021         /* we should read data for the packet */
1022         if
1023                 (l_read_data)
1024         {
1025                 l_nb_bytes_read = 0;
1026                 if
1027                         (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info))
1028                 {
1029                         return OPJ_FALSE;
1030                 }
1031                 l_nb_total_bytes_read += l_nb_bytes_read;
1032         }
1033         *p_data_read = l_nb_total_bytes_read;
1034         return OPJ_FALSE;
1035 }
1036
1037 static opj_bool t2_skip_packet(
1038                                                          opj_t2_v2_t* p_t2,
1039                                                          opj_tcd_tile_t *p_tile,
1040                              opj_tcp_v2_t *p_tcp,
1041                                                          opj_pi_iterator_t *p_pi,
1042                                                          OPJ_BYTE *p_src,
1043                                                          OPJ_UINT32 * p_data_read,
1044                                                          OPJ_UINT32 p_max_length,
1045                                                          opj_packet_info_t *p_pack_info)
1046 {
1047         opj_bool l_read_data;
1048         OPJ_UINT32 l_nb_bytes_read = 0;
1049         OPJ_UINT32 l_nb_total_bytes_read = 0;
1050
1051         *p_data_read = 0;
1052
1053         if
1054                 (! t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info))
1055         {
1056                 return OPJ_FALSE;
1057         }
1058         p_src += l_nb_bytes_read;
1059         l_nb_total_bytes_read += l_nb_bytes_read;
1060         p_max_length -= l_nb_bytes_read;
1061         /* we should read data for the packet */
1062         if
1063                 (l_read_data)
1064         {
1065                 l_nb_bytes_read = 0;
1066                 if
1067                         (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info))
1068                 {
1069                         return OPJ_FALSE;
1070                 }
1071                 l_nb_total_bytes_read += l_nb_bytes_read;
1072         }
1073         *p_data_read = l_nb_total_bytes_read;
1074         return OPJ_TRUE;
1075 }
1076
1077
1078
1079 static opj_bool t2_read_packet_header(
1080                                                          opj_t2_v2_t* p_t2,
1081                                                          opj_tcd_tile_t *p_tile,
1082                              opj_tcp_v2_t *p_tcp,
1083                                                          opj_pi_iterator_t *p_pi,
1084                                                          opj_bool * p_is_data_present,
1085                                                          OPJ_BYTE *p_src_data,
1086                                                          OPJ_UINT32 * p_data_read,
1087                                                          OPJ_UINT32 p_max_length,
1088                                                          opj_packet_info_t *p_pack_info)
1089 {
1090         /* loop */
1091         OPJ_UINT32 bandno, cblkno;
1092         OPJ_UINT32 l_nb_code_blocks;
1093         OPJ_UINT32 l_remaining_length;
1094         OPJ_UINT32 l_header_length;
1095         OPJ_UINT32 * l_modified_length_ptr = 00;
1096         OPJ_BYTE *l_current_data = p_src_data;
1097         opj_cp_v2_t *l_cp = p_t2->cp;
1098         opj_bio_t *l_bio = 00;  /* BIO component */
1099         opj_tcd_band_t *l_band = 00;
1100         opj_tcd_cblk_dec_t* l_cblk = 00;
1101         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1102
1103         OPJ_BYTE *l_header_data = 00;
1104         OPJ_BYTE **l_header_data_start = 00;
1105
1106         OPJ_UINT32 l_present;
1107
1108         if
1109                 (p_pi->layno == 0)
1110         {
1111                 l_band = l_res->bands;
1112                 /* reset tagtrees */
1113                 for
1114                         (bandno = 0; bandno < l_res->numbands; ++bandno)
1115                 {
1116                         opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1117
1118                         if (
1119                                 ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)))
1120                         {
1121                                 tgt_reset(l_prc->incltree);
1122                                 tgt_reset(l_prc->imsbtree);
1123                                 l_cblk = l_prc->cblks.dec;
1124                                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1125                                 for
1126                                         (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
1127                                 {
1128                                         l_cblk->numsegs = 0;
1129                                         l_cblk->real_num_segs = 0;
1130                                         ++l_cblk;
1131                                 }
1132                         }
1133                         ++l_band;
1134                 }
1135         }
1136
1137         /* SOP markers */
1138
1139         if (p_tcp->csty & J2K_CP_CSTY_SOP) {
1140                 if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
1141                         // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n");
1142                 } else {
1143                         l_current_data += 6;
1144                 }
1145
1146                 /** TODO : check the Nsop value */
1147         }
1148
1149         /*
1150         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
1151         This part deal with this caracteristic
1152         step 1: Read packet header in the saved structure
1153         step 2: Return to codestream for decoding
1154         */
1155
1156         l_bio = bio_create();
1157         if
1158                 (! l_bio)
1159         {
1160                 return OPJ_FALSE;
1161         }
1162
1163         if
1164                 (l_cp->ppm == 1)
1165         {               /* PPM */
1166                 l_header_data_start = &l_cp->ppm_data;
1167                 l_header_data = *l_header_data_start;
1168                 l_modified_length_ptr = &(l_cp->ppm_len);
1169
1170         }
1171         else if
1172                 (p_tcp->ppt == 1)
1173         {       /* PPT */
1174                 l_header_data_start = &(p_tcp->ppt_data);
1175                 l_header_data = *l_header_data_start;
1176                 l_modified_length_ptr = &(p_tcp->ppt_len);
1177         }
1178         else
1179         {       /* Normal Case */
1180                 l_header_data_start = &(l_current_data);
1181                 l_header_data = *l_header_data_start;
1182                 l_remaining_length = p_src_data+p_max_length-l_header_data;
1183                 l_modified_length_ptr = &(l_remaining_length);
1184         }
1185         bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
1186         l_present = bio_read(l_bio, 1);
1187         if
1188                 (!l_present)
1189         {
1190                 bio_inalign(l_bio);
1191                 l_header_data += bio_numbytes(l_bio);
1192                 bio_destroy(l_bio);
1193                 /* EPH markers */
1194                 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1195                         if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1196                                 printf("Error : expected EPH marker\n");
1197                         } else {
1198                                 l_header_data += 2;
1199                         }
1200                 }
1201                 l_header_length = (l_header_data - *l_header_data_start);
1202                 *l_modified_length_ptr -= l_header_length;
1203                 *l_header_data_start += l_header_length;
1204                 /* << INDEX */
1205                 // End of packet header position. Currently only represents the distance to start of packet
1206                 // Will be updated later by incrementing with packet start value
1207                 if
1208                         (p_pack_info)
1209                 {
1210                         p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1211                 }
1212                 /* INDEX >> */
1213                 * p_is_data_present = OPJ_FALSE;
1214                 *p_data_read = l_current_data - p_src_data;
1215                 return OPJ_TRUE;
1216         }
1217
1218         l_band = l_res->bands;
1219         for
1220                 (bandno = 0; bandno < l_res->numbands; ++bandno)
1221         {
1222                 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
1223
1224                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
1225                 {
1226                         ++l_band;
1227                         continue;
1228                 }
1229                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1230                 l_cblk = l_prc->cblks.dec;
1231                 for
1232                         (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++)
1233                 {
1234                         OPJ_UINT32 l_included,l_increment, l_segno;
1235                         OPJ_INT32 n;
1236                         /* if cblk not yet included before --> inclusion tagtree */
1237                         if
1238                                 (!l_cblk->numsegs)
1239                         {
1240                                 l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1);
1241                                 /* else one bit */
1242                         }
1243                         else
1244                         {
1245                                 l_included = bio_read(l_bio, 1);
1246                         }
1247                         /* if cblk not included */
1248                         if
1249                                 (!l_included)
1250                         {
1251                                 l_cblk->numnewpasses = 0;
1252                                 ++l_cblk;
1253                                 continue;
1254                         }
1255                         /* if cblk not yet included --> zero-bitplane tagtree */
1256                         if
1257                                 (!l_cblk->numsegs)
1258                         {
1259                                 OPJ_UINT32 i = 0;
1260                                 while
1261                                         (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i))
1262                                 {
1263                                         ++i;
1264                                 }
1265                                 l_cblk->numbps = l_band->numbps + 1 - i;
1266                                 l_cblk->numlenbits = 3;
1267                         }
1268                         /* number of coding passes */
1269                         l_cblk->numnewpasses = t2_getnumpasses(l_bio);
1270                         l_increment = t2_getcommacode(l_bio);
1271                         /* length indicator increment */
1272                         l_cblk->numlenbits += l_increment;
1273                         l_segno = 0;
1274                         if
1275                                 (!l_cblk->numsegs)
1276                         {
1277                                 if
1278                                         (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1))
1279                                 {
1280                                         bio_destroy(l_bio);
1281                                         return OPJ_FALSE;
1282                                 }
1283
1284                         }
1285                         else
1286                         {
1287                                 l_segno = l_cblk->numsegs - 1;
1288                                 if
1289                                         (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses)
1290                                 {
1291                                         ++l_segno;
1292                                         if
1293                                                 (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0))
1294                                         {
1295                                                 bio_destroy(l_bio);
1296                                                 return OPJ_FALSE;
1297                                         }
1298                                 }
1299                         }
1300                         n = l_cblk->numnewpasses;
1301
1302                         do {
1303                                 l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n);
1304                                 l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1305                                 n -= l_cblk->segs[l_segno].numnewpasses;
1306                                 if
1307                                         (n > 0)
1308                                 {
1309                                         ++l_segno;
1310                                         if
1311                                                 (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0))
1312                                         {
1313                                                 bio_destroy(l_bio);
1314                                                 return OPJ_FALSE;
1315                                         }
1316                                 }
1317                         }
1318                         while (n > 0);
1319                         ++l_cblk;
1320                 }
1321                 ++l_band;
1322         }
1323
1324         if
1325                 (bio_inalign(l_bio))
1326         {
1327                 bio_destroy(l_bio);
1328                 return OPJ_FALSE;
1329         }
1330
1331         l_header_data += bio_numbytes(l_bio);
1332         bio_destroy(l_bio);
1333
1334         /* EPH markers */
1335         if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1336                 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1337                         // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n");
1338                 } else {
1339                         l_header_data += 2;
1340                 }
1341         }
1342
1343
1344         l_header_length = (l_header_data - *l_header_data_start);
1345         *l_modified_length_ptr -= l_header_length;
1346         *l_header_data_start += l_header_length;
1347         /* << INDEX */
1348         // End of packet header position. Currently only represents the distance to start of packet
1349         // Will be updated later by incrementing with packet start value
1350         if
1351                 (p_pack_info)
1352         {
1353                 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1354         }
1355         /* INDEX >> */
1356         * p_is_data_present = OPJ_TRUE;
1357         *p_data_read = l_current_data - p_src_data;
1358         return OPJ_TRUE;
1359 }
1360
1361 static opj_bool t2_read_packet_data(
1362                                                          opj_t2_v2_t* p_t2,
1363                                                          opj_tcd_tile_t *p_tile,
1364                                                          opj_pi_iterator_t *p_pi,
1365                                                          OPJ_BYTE *p_src_data,
1366                                                          OPJ_UINT32 * p_data_read,
1367                                                          OPJ_UINT32 p_max_length,
1368                                                          opj_packet_info_t *pack_info)
1369 {
1370         OPJ_UINT32 bandno, cblkno;
1371         OPJ_UINT32 l_nb_code_blocks;
1372         OPJ_BYTE *l_current_data = p_src_data;
1373         opj_tcd_band_t *l_band = 00;
1374         opj_tcd_cblk_dec_t* l_cblk = 00;
1375         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1376
1377         l_band = l_res->bands;
1378         for
1379                 (bandno = 0; bandno < l_res->numbands; ++bandno)
1380         {
1381                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1382
1383                 if
1384                         ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
1385                 {
1386                         ++l_band;
1387                         continue;
1388                 }
1389                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1390                 l_cblk = l_prc->cblks.dec;
1391                 for
1392                         (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
1393                 {
1394                         opj_tcd_seg_t *l_seg = 00;
1395                         if
1396                                 (!l_cblk->numnewpasses)
1397                         {
1398                                 /* nothing to do */
1399                                 ++l_cblk;
1400                                 continue;
1401                         }
1402                         if
1403                                 (!l_cblk->numsegs)
1404                         {
1405                                 l_seg = l_cblk->segs;
1406                                 ++l_cblk->numsegs;
1407                                 l_cblk->len = 0;
1408                         }
1409                         else
1410                         {
1411                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1412                                 if
1413                                         (l_seg->numpasses == l_seg->maxpasses)
1414                                 {
1415                                         ++l_seg;
1416                                         ++l_cblk->numsegs;
1417                                 }
1418                         }
1419
1420                         do
1421                         {
1422                                 if
1423                                         (l_current_data + l_seg->newlen > p_src_data + p_max_length)
1424                                 {
1425                                         return OPJ_FALSE;
1426                                 }
1427
1428 #ifdef USE_JPWL
1429                         /* we need here a j2k handle to verify if making a check to
1430                         the validity of cblocks parameters is selected from user (-W) */
1431
1432                                 /* let's check that we are not exceeding */
1433                                 if ((cblk->len + seg->newlen) > 8192) {
1434                                         opj_event_msg(t2->cinfo, EVT_WARNING,
1435                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1436                                                 seg->newlen, cblkno, precno, bandno, resno, compno);
1437                                         if (!JPWL_ASSUME) {
1438                                                 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1439                                                 return -999;
1440                                         }
1441                                         seg->newlen = 8192 - cblk->len;
1442                                         opj_event_msg(t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", seg->newlen);
1443                                         break;
1444                                 };
1445
1446 #endif /* USE_JPWL */
1447
1448                                 memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen);
1449                                 if
1450                                         (l_seg->numpasses == 0)
1451                                 {
1452                                         l_seg->data = &l_cblk->data;
1453                                         l_seg->dataindex = l_cblk->len;
1454                                 }
1455                                 l_current_data += l_seg->newlen;
1456                                 l_seg->numpasses += l_seg->numnewpasses;
1457                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1458
1459                                 l_seg->real_num_passes = l_seg->numpasses;
1460                                 l_cblk->len += l_seg->newlen;
1461                                 l_seg->len += l_seg->newlen;
1462                                 if
1463                                         (l_cblk->numnewpasses > 0)
1464                                 {
1465                                         ++l_seg;
1466                                         ++l_cblk->numsegs;
1467                                 }
1468                         }
1469                         while (l_cblk->numnewpasses > 0);
1470                         l_cblk->real_num_segs = l_cblk->numsegs;
1471                         ++l_cblk;
1472                 }
1473                 ++l_band;
1474         }
1475         *(p_data_read) = l_current_data - p_src_data;
1476         return OPJ_TRUE;
1477 }
1478
1479 static opj_bool t2_skip_packet_data(
1480                                                          opj_t2_v2_t* p_t2,
1481                                                          opj_tcd_tile_t *p_tile,
1482                                                          opj_pi_iterator_t *p_pi,
1483                                                          OPJ_UINT32 * p_data_read,
1484                                                          OPJ_UINT32 p_max_length,
1485                                                          opj_packet_info_t *pack_info)
1486 {
1487         OPJ_UINT32 bandno, cblkno;
1488         OPJ_UINT32 l_nb_code_blocks;
1489         opj_tcd_band_t *l_band = 00;
1490         opj_tcd_cblk_dec_t* l_cblk = 00;
1491
1492         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1493
1494         *p_data_read = 0;
1495         l_band = l_res->bands;
1496         for
1497                 (bandno = 0; bandno < l_res->numbands; ++bandno)
1498         {
1499                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1500
1501                 if
1502                         ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
1503                 {
1504                         ++l_band;
1505                         continue;
1506                 }
1507                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1508                 l_cblk = l_prc->cblks.dec;
1509                 for
1510                         (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
1511                 {
1512                         opj_tcd_seg_t *l_seg = 00;
1513                         if
1514                                 (!l_cblk->numnewpasses)
1515                         {
1516                                 /* nothing to do */
1517                                 ++l_cblk;
1518                                 continue;
1519                         }
1520                         if
1521                                 (!l_cblk->numsegs)
1522                         {
1523                                 l_seg = l_cblk->segs;
1524                                 ++l_cblk->numsegs;
1525                                 l_cblk->len = 0;
1526                         }
1527                         else
1528                         {
1529                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1530                                 if
1531                                         (l_seg->numpasses == l_seg->maxpasses)
1532                                 {
1533                                         ++l_seg;
1534                                         ++l_cblk->numsegs;
1535                                 }
1536                         }
1537
1538                         do
1539                         {
1540                                 if
1541                                         (* p_data_read + l_seg->newlen > p_max_length)
1542                                 {
1543                                         return OPJ_FALSE;
1544                                 }
1545
1546 #ifdef USE_JPWL
1547                         /* we need here a j2k handle to verify if making a check to
1548                         the validity of cblocks parameters is selected from user (-W) */
1549
1550                                 /* let's check that we are not exceeding */
1551                                 if ((cblk->len + seg->newlen) > 8192) {
1552                                         opj_event_msg(t2->cinfo, EVT_WARNING,
1553                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1554                                                 seg->newlen, cblkno, precno, bandno, resno, compno);
1555                                         if (!JPWL_ASSUME) {
1556                                                 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1557                                                 return -999;
1558                                         }
1559                                         seg->newlen = 8192 - cblk->len;
1560                                         opj_event_msg(t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", seg->newlen);
1561                                         break;
1562                                 };
1563
1564 #endif /* USE_JPWL */
1565                                 *(p_data_read) += l_seg->newlen;
1566                                 l_seg->numpasses += l_seg->numnewpasses;
1567                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1568                                 if
1569                                         (l_cblk->numnewpasses > 0)
1570                                 {
1571                                         ++l_seg;
1572                                         ++l_cblk->numsegs;
1573                                 }
1574                         }
1575                         while (l_cblk->numnewpasses > 0);
1576                         ++l_cblk;
1577                 }
1578                 ++l_band;
1579         }
1580         return OPJ_TRUE;
1581 }
1582
1583
1584 static opj_bool t2_init_seg_v2(opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first)
1585 {
1586         opj_tcd_seg_t* seg = 00;
1587         OPJ_UINT32 l_nb_segs = index + 1;
1588
1589         if
1590                 (l_nb_segs > cblk->m_current_max_segs)
1591         {
1592                 cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS;
1593         cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1594                 if
1595                         (! cblk->segs)
1596                 {
1597                         return OPJ_FALSE;
1598                 }
1599         }
1600         seg = &cblk->segs[index];
1601         memset(seg,0,sizeof(opj_tcd_seg_t));
1602
1603         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1604                 seg->maxpasses = 1;
1605         }
1606         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1607                 if (first) {
1608                         seg->maxpasses = 10;
1609                 } else {
1610                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1611                 }
1612         } else {
1613                 seg->maxpasses = 109;
1614         }
1615         return OPJ_TRUE;
1616 }