[trunk] modify image_to_j2k and the lib to support functionalities given by the v2...
[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 /**
63 Encode a packet of a tile to a destination buffer
64 @param tile Tile for which to write the packets
65 @param tcp Tile coding parameters
66 @param pi Packet identity
67 @param dest Destination buffer
68 @param len Length of the destination buffer
69 @param cstr_info Codestream information structure
70 @param tileno Number of the tile encoded
71 @return
72 */
73 static opj_bool t2_encode_packet_v2(
74                                                          OPJ_UINT32 tileno,
75                                                          opj_tcd_tile_v2_t *tile,
76                                                          opj_tcp_v2_t *tcp,
77                                                          opj_pi_iterator_t *pi,
78                                                          OPJ_BYTE *dest,
79                                                          OPJ_UINT32 * p_data_written,
80                                                          OPJ_UINT32 len,
81                                                          opj_codestream_info_t *cstr_info);
82
83 /**
84 @param cblk
85 @param index
86 @param cblksty
87 @param first
88 */
89 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first);
90 /**
91 Decode a packet of a tile from a source buffer
92 @param t2 T2 handle
93 @param src Source buffer
94 @param len Length of the source buffer
95 @param tile Tile for which to write the packets
96 @param tcp Tile coding parameters
97 @param pi Packet identity
98 @param pack_info Packet information
99 @return 
100 */
101 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, 
102                                                                                                                 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info);
103
104
105 /**
106 Decode a packet of a tile from a source buffer
107 @param t2 T2 handle
108 @param tile Tile for which to write the packets
109 @param tcp Tile coding parameters
110 @param pi Packet identity
111 @param src Source buffer
112 @param pack_info Packet information
113
114 @return
115 */
116 static opj_bool t2_decode_packet_v2(
117                                                          opj_t2_v2_t* t2,
118                                                          opj_tcd_tile_v2_t *tile,
119                              opj_tcp_v2_t *tcp,
120                                                          opj_pi_iterator_t *pi,
121                                                          OPJ_BYTE *src,
122                                                          OPJ_UINT32 * data_read,
123                                                          OPJ_UINT32 max_length,
124                                                          opj_packet_info_t *pack_info);
125
126 static opj_bool t2_skip_packet(
127                                                          opj_t2_v2_t* p_t2,
128                                                          opj_tcd_tile_v2_t *p_tile,
129                              opj_tcp_v2_t *p_tcp,
130                                                          opj_pi_iterator_t *p_pi,
131                                                          OPJ_BYTE *p_src,
132                                                          OPJ_UINT32 * p_data_read,
133                                                          OPJ_UINT32 p_max_length,
134                                                          opj_packet_info_t *p_pack_info);
135
136 static opj_bool t2_read_packet_header(
137                                                          opj_t2_v2_t* p_t2,
138                                                          opj_tcd_tile_v2_t *p_tile,
139                              opj_tcp_v2_t *p_tcp,
140                                                          opj_pi_iterator_t *p_pi,
141                                                          opj_bool * p_is_data_present,
142                                                          OPJ_BYTE *p_src_data,
143                                                          OPJ_UINT32 * p_data_read,
144                                                          OPJ_UINT32 p_max_length,
145                                                          opj_packet_info_t *p_pack_info);
146
147 static opj_bool t2_read_packet_data(
148                                                          opj_t2_v2_t* p_t2,
149                                                          opj_tcd_tile_v2_t *p_tile,
150                                                          opj_pi_iterator_t *p_pi,
151                                                          OPJ_BYTE *p_src_data,
152                                                          OPJ_UINT32 * p_data_read,
153                                                          OPJ_UINT32 p_max_length,
154                                                          opj_packet_info_t *pack_info);
155
156 static opj_bool t2_skip_packet_data(
157                                                          opj_t2_v2_t* p_t2,
158                                                          opj_tcd_tile_v2_t *p_tile,
159                                                          opj_pi_iterator_t *p_pi,
160                                                          OPJ_UINT32 * p_data_read,
161                                                          OPJ_UINT32 p_max_length,
162                                                          opj_packet_info_t *pack_info);
163
164 /**
165 @param cblk
166 @param index
167 @param cblksty
168 @param first
169 */
170 static opj_bool t2_init_seg_v2( opj_tcd_cblk_dec_v2_t* cblk,
171                                                                 OPJ_UINT32 index,
172                                                                 OPJ_UINT32 cblksty,
173                                                                 OPJ_UINT32 first);
174
175 /*@}*/
176
177 /*@}*/
178
179 /* ----------------------------------------------------------------------- */
180
181 /* #define RESTART 0x04 */
182
183 static void t2_putcommacode(opj_bio_t *bio, int n) {
184         while (--n >= 0) {
185                 bio_write(bio, 1, 1);
186         }
187         bio_write(bio, 0, 1);
188 }
189
190 static int t2_getcommacode(opj_bio_t *bio) {
191         int n;
192         for (n = 0; bio_read(bio, 1); n++) {
193                 ;
194         }
195         return n;
196 }
197
198 static void t2_putnumpasses(opj_bio_t *bio, int n) {
199         if (n == 1) {
200                 bio_write(bio, 0, 1);
201         } else if (n == 2) {
202                 bio_write(bio, 2, 2);
203         } else if (n <= 5) {
204                 bio_write(bio, 0xc | (n - 3), 4);
205         } else if (n <= 36) {
206                 bio_write(bio, 0x1e0 | (n - 6), 9);
207         } else if (n <= 164) {
208                 bio_write(bio, 0xff80 | (n - 37), 16);
209         }
210 }
211
212 static int t2_getnumpasses(opj_bio_t *bio) {
213         int n;
214         if (!bio_read(bio, 1))
215                 return 1;
216         if (!bio_read(bio, 1))
217                 return 2;
218         if ((n = bio_read(bio, 2)) != 3)
219                 return (3 + n);
220         if ((n = bio_read(bio, 5)) != 31)
221                 return (6 + n);
222         return (37 + bio_read(bio, 7));
223 }
224
225 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) {
226         int bandno, cblkno;
227         unsigned char *c = dest;
228
229         int compno = pi->compno;        /* component value */
230         int resno  = pi->resno;         /* resolution level value */
231         int precno = pi->precno;        /* precinct value */
232         int layno  = pi->layno;         /* quality layer value */
233
234         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
235         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
236         
237         opj_bio_t *bio = NULL;  /* BIO component */
238         
239         /* <SOP 0xff91> */
240         if (tcp->csty & J2K_CP_CSTY_SOP) {
241                 c[0] = 255;
242                 c[1] = 145;
243                 c[2] = 0;
244                 c[3] = 4;
245                 c[4] = (unsigned char)((tile->packno % 65536) / 256);
246                 c[5] = (unsigned char)((tile->packno % 65536) % 256);
247                 c += 6;
248         }
249         /* </SOP> */
250         
251         if (!layno) {
252                 for (bandno = 0; bandno < res->numbands; bandno++) {
253                         opj_tcd_band_t *band = &res->bands[bandno];
254                         opj_tcd_precinct_t *prc = &band->precincts[precno];
255                         tgt_reset(prc->incltree);
256                         tgt_reset(prc->imsbtree);
257                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
258                                 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
259                                 cblk->numpasses = 0;
260                                 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
261                         }
262                 }
263         }
264         
265         bio = bio_create();
266         bio_init_enc(bio, c, length);
267         bio_write(bio, 1, 1);           /* Empty header bit */
268         
269         /* Writing Packet header */
270         for (bandno = 0; bandno < res->numbands; bandno++) {
271                 opj_tcd_band_t *band = &res->bands[bandno];
272                 opj_tcd_precinct_t *prc = &band->precincts[precno];
273                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
274                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
275                         opj_tcd_layer_t *layer = &cblk->layers[layno];
276                         if (!cblk->numpasses && layer->numpasses) {
277                                 tgt_setvalue(prc->incltree, cblkno, layno);
278                         }
279                 }
280                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
281                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
282                         opj_tcd_layer_t *layer = &cblk->layers[layno];
283                         int increment = 0;
284                         int nump = 0;
285                         int len = 0, passno;
286                         /* cblk inclusion bits */
287                         if (!cblk->numpasses) {
288                                 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
289                         } else {
290                                 bio_write(bio, layer->numpasses != 0, 1);
291                         }
292                         /* if cblk not included, go to the next cblk  */
293                         if (!layer->numpasses) {
294                                 continue;
295                         }
296                         /* if first instance of cblk --> zero bit-planes information */
297                         if (!cblk->numpasses) {
298                                 cblk->numlenbits = 3;
299                                 tgt_encode(bio, prc->imsbtree, cblkno, 999);
300                         }
301                         /* number of coding passes included */
302                         t2_putnumpasses(bio, layer->numpasses);
303                         
304                         /* computation of the increase of the length indicator and insertion in the header     */
305                         for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
306                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
307                                 nump++;
308                                 len += pass->len;
309                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
310                                         increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
311                                         len = 0;
312                                         nump = 0;
313                                 }
314                         }
315                         t2_putcommacode(bio, increment);
316
317                         /* computation of the new Length indicator */
318                         cblk->numlenbits += increment;
319
320                         /* insertion of the codeword segment length */
321                         for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
322                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
323                                 nump++;
324                                 len += pass->len;
325                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
326                                         bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
327                                         len = 0;
328                                         nump = 0;
329                                 }
330                         }
331                 }
332         }
333
334         if (bio_flush(bio)) {
335                 bio_destroy(bio);
336                 return -999;            /* modified to eliminate longjmp !! */
337         }
338
339         c += bio_numbytes(bio);
340         bio_destroy(bio);
341         
342         /* <EPH 0xff92> */
343         if (tcp->csty & J2K_CP_CSTY_EPH) {
344                 c[0] = 255;
345                 c[1] = 146;
346                 c += 2;
347         }
348         /* </EPH> */
349
350         /* << INDEX */
351         /* End of packet header position. Currently only represents the distance to start of packet
352         // Will be updated later by incrementing with packet start value */
353         if(cstr_info && cstr_info->index_write) {
354                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
355                 info_PK->end_ph_pos = (int)(c - dest);
356         }
357         /* INDEX >> */
358         
359         /* Writing the packet body */
360         
361         for (bandno = 0; bandno < res->numbands; bandno++) {
362                 opj_tcd_band_t *band = &res->bands[bandno];
363                 opj_tcd_precinct_t *prc = &band->precincts[precno];
364                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
365                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
366                         opj_tcd_layer_t *layer = &cblk->layers[layno];
367                         if (!layer->numpasses) {
368                                 continue;
369                         }
370                         if (c + layer->len > dest + length) {
371                                 return -999;
372                         }
373                         
374                         memcpy(c, layer->data, layer->len);
375                         cblk->numpasses += layer->numpasses;
376                         c += layer->len;
377                         /* << INDEX */ 
378                         if(cstr_info && cstr_info->index_write) {
379                                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
380                                 info_PK->disto += layer->disto;
381                                 if (cstr_info->D_max < info_PK->disto) {
382                                         cstr_info->D_max = info_PK->disto;
383                                 }
384                         }
385                         /* INDEX >> */
386                 }
387         }
388         
389         return (c - dest);
390 }
391
392 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) {
393         opj_tcd_seg_t* seg;
394         cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
395         seg = &cblk->segs[index];
396         seg->data = NULL;
397         seg->dataindex = 0;
398         seg->numpasses = 0;
399         seg->len = 0;
400         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
401                 seg->maxpasses = 1;
402         }
403         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
404                 if (first) {
405                         seg->maxpasses = 10;
406                 } else {
407                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
408                 }
409         } else {
410                 seg->maxpasses = 109;
411         }
412 }
413
414 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, 
415                                                                                                                 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
416         int bandno, cblkno;
417         unsigned char *c = src;
418
419         opj_cp_t *cp = t2->cp;
420
421         int compno = pi->compno;        /* component value */
422         int resno  = pi->resno;         /* resolution level value */
423         int precno = pi->precno;        /* precinct value */
424         int layno  = pi->layno;         /* quality layer value */
425
426         opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno];
427
428         unsigned char *hd = NULL;
429         int present;
430         
431         opj_bio_t *bio = NULL;  /* BIO component */
432         
433         if (layno == 0) {
434                 for (bandno = 0; bandno < res->numbands; bandno++) {
435                         opj_tcd_band_t *band = &res->bands[bandno];
436                         opj_tcd_precinct_t *prc = &band->precincts[precno];
437                         
438                         if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
439                         
440                         tgt_reset(prc->incltree);
441                         tgt_reset(prc->imsbtree);
442                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
443                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
444                                 cblk->numsegs = 0;
445                         }
446                 }
447         }
448         
449         /* SOP markers */
450         
451         if (tcp->csty & J2K_CP_CSTY_SOP) {
452                 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
453                         opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
454                 } else {
455                         c += 6;
456                 }
457                 
458                 /** TODO : check the Nsop value */
459         }
460         
461         /* 
462         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
463         This part deal with this caracteristic
464         step 1: Read packet header in the saved structure
465         step 2: Return to codestream for decoding 
466         */
467
468         bio = bio_create();
469         
470         if (cp->ppm == 1) {             /* PPM */
471                 hd = cp->ppm_data;
472                 bio_init_dec(bio, hd, cp->ppm_len);
473         } else if (tcp->ppt == 1) {     /* PPT */
474                 hd = tcp->ppt_data;
475                 bio_init_dec(bio, hd, tcp->ppt_len);
476         } else {                        /* Normal Case */
477                 hd = c;
478                 bio_init_dec(bio, hd, src+len-hd);
479         }
480         
481         present = bio_read(bio, 1);
482         
483         if (!present) {
484                 bio_inalign(bio);
485                 hd += bio_numbytes(bio);
486                 bio_destroy(bio);
487                 
488                 /* EPH markers */
489                 
490                 if (tcp->csty & J2K_CP_CSTY_EPH) {
491                         if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
492                                 printf("Error : expected EPH marker\n");
493                         } else {
494                                 hd += 2;
495                         }
496                 }
497
498                 /* << INDEX */
499                 /* End of packet header position. Currently only represents the distance to start of packet
500                 // Will be updated later by incrementing with packet start value*/
501                 if(pack_info) {
502                         pack_info->end_ph_pos = (int)(c - src);
503                 }
504                 /* INDEX >> */
505                 
506                 if (cp->ppm == 1) {             /* PPM case */
507                         cp->ppm_len += cp->ppm_data-hd;
508                         cp->ppm_data = hd;
509                         return (c - src);
510                 }
511                 if (tcp->ppt == 1) {    /* PPT case */
512                         tcp->ppt_len+=tcp->ppt_data-hd;
513                         tcp->ppt_data = hd;
514                         return (c - src);
515                 }
516                 
517                 return (hd - src);
518         }
519         
520         for (bandno = 0; bandno < res->numbands; bandno++) {
521                 opj_tcd_band_t *band = &res->bands[bandno];
522                 opj_tcd_precinct_t *prc = &band->precincts[precno];
523                 
524                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
525                 
526                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
527                         int included, increment, n, segno;
528                         opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
529                         /* if cblk not yet included before --> inclusion tagtree */
530                         if (!cblk->numsegs) {
531                                 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
532                                 /* else one bit */
533                         } else {
534                                 included = bio_read(bio, 1);
535                         }
536                         /* if cblk not included */
537                         if (!included) {
538                                 cblk->numnewpasses = 0;
539                                 continue;
540                         }
541                         /* if cblk not yet included --> zero-bitplane tagtree */
542                         if (!cblk->numsegs) {
543                                 int i, numimsbs;
544                                 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
545                                         ;
546                                 }
547                                 numimsbs = i - 1;
548                                 cblk->numbps = band->numbps - numimsbs;
549                                 cblk->numlenbits = 3;
550                         }
551                         /* number of coding passes */
552                         cblk->numnewpasses = t2_getnumpasses(bio);
553                         increment = t2_getcommacode(bio);
554                         /* length indicator increment */
555                         cblk->numlenbits += increment;
556                         segno = 0;
557                         if (!cblk->numsegs) {
558                                 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1);
559                         } else {
560                                 segno = cblk->numsegs - 1;
561                                 if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
562                                         ++segno;
563                                         t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
564                                 }
565                         }
566                         n = cblk->numnewpasses;
567                         
568                         do {
569                                 cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
570                                 cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
571                                 n -= cblk->segs[segno].numnewpasses;
572                                 if (n > 0) {
573                                         ++segno;
574                                         t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
575                                 }
576                         } while (n > 0);
577                 }
578         }
579         
580         if (bio_inalign(bio)) {
581                 bio_destroy(bio);
582                 return -999;
583         }
584         
585         hd += bio_numbytes(bio);
586         bio_destroy(bio);
587         
588         /* EPH markers */
589         if (tcp->csty & J2K_CP_CSTY_EPH) {
590                 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
591                         opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
592                         return -999;
593                 } else {
594                         hd += 2;
595                 }
596         }
597
598         /* << INDEX */
599         /* End of packet header position. Currently only represents the distance to start of packet
600         // Will be updated later by incrementing with packet start value*/
601         if(pack_info) {
602                 pack_info->end_ph_pos = (int)(hd - src);
603         }
604         /* INDEX >> */
605         
606         if (cp->ppm==1) {
607                 cp->ppm_len+=cp->ppm_data-hd;
608                 cp->ppm_data = hd;
609         } else if (tcp->ppt == 1) {
610                 tcp->ppt_len+=tcp->ppt_data-hd;
611                 tcp->ppt_data = hd;
612         } else {
613                 c=hd;
614         }
615         
616         for (bandno = 0; bandno < res->numbands; bandno++) {
617                 opj_tcd_band_t *band = &res->bands[bandno];
618                 opj_tcd_precinct_t *prc = &band->precincts[precno];
619                 
620                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
621                 
622                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
623                         opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
624                         opj_tcd_seg_t *seg = NULL;
625                         if (!cblk->numnewpasses)
626                                 continue;
627                         if (!cblk->numsegs) {
628                                 seg = &cblk->segs[0];
629                                 cblk->numsegs++;
630                                 cblk->len = 0;
631                         } else {
632                                 seg = &cblk->segs[cblk->numsegs - 1];
633                                 if (seg->numpasses == seg->maxpasses) {
634                                         seg++;
635                                         cblk->numsegs++;
636                                 }
637                         }
638                         
639                         do {
640                                 if (c + seg->newlen > src + len) {
641                                         return -999;
642                                 }
643
644 #ifdef USE_JPWL
645                         /* we need here a j2k handle to verify if making a check to
646                         the validity of cblocks parameters is selected from user (-W) */
647
648                                 /* let's check that we are not exceeding */
649                                 if ((cblk->len + seg->newlen) > 8192) {
650                                         opj_event_msg(t2->cinfo, EVT_WARNING,
651                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
652                                                 seg->newlen, cblkno, precno, bandno, resno, compno);
653                                         if (!JPWL_ASSUME) {
654                                                 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
655                                                 return -999;
656                                         }
657                                         seg->newlen = 8192 - cblk->len;
658                                         opj_event_msg(t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", seg->newlen);
659                                         break;
660                                 };
661
662 #endif /* USE_JPWL */
663                                 
664                                 cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char));
665                                 memcpy(cblk->data + cblk->len, c, seg->newlen);
666                                 if (seg->numpasses == 0) {
667                                         seg->data = &cblk->data;
668                                         seg->dataindex = cblk->len;
669                                 }
670                                 c += seg->newlen;
671                                 cblk->len += seg->newlen;
672                                 seg->len += seg->newlen;
673                                 seg->numpasses += seg->numnewpasses;
674                                 cblk->numnewpasses -= seg->numnewpasses;
675                                 if (cblk->numnewpasses > 0) {
676                                         seg++;
677                                         cblk->numsegs++;
678                                 }
679                         } while (cblk->numnewpasses > 0);
680                 }
681         }
682         
683         return (c - src);
684 }
685
686 /* ----------------------------------------------------------------------- */
687
688 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){
689         unsigned char *c = dest;
690         int e = 0;
691         int compno;
692         opj_pi_iterator_t *pi = NULL;
693         int poc;
694         opj_image_t *image = t2->image;
695         opj_cp_t *cp = t2->cp;
696         opj_tcp_t *tcp = &cp->tcps[tileno];
697         int pocno = cp->cinema == CINEMA4K_24? 2: 1;
698         int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
699         
700         pi = pi_initialise_encode(image, cp, tileno, t2_mode);
701         if(!pi) {
702                 /* TODO: throw an error */
703                 return -999;
704         }
705         
706         if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
707                 for(compno = 0; compno < maxcomp; compno++ ){
708                         for(poc = 0; poc < pocno ; poc++){
709                                 int comp_len = 0;
710                                 int tpnum = compno;
711                                 if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
712                                         opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
713                                         pi_destroy(pi, cp, tileno);
714                                         return -999;
715                                 }
716                                 while (pi_next(&pi[poc])) {
717                                         if (pi[poc].layno < maxlayers) {
718                                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
719                                                 comp_len = comp_len + e;
720                                                 if (e == -999) {
721                                                         break;
722                                                 } else {
723                                                         c += e;
724                                                 }
725                                         }
726                                 }
727                                 if (e == -999) break;
728                                 if (cp->max_comp_size){
729                                         if (comp_len > cp->max_comp_size){
730                                                 e = -999;
731                                                 break;
732                                         }
733                                 }
734                         }
735                         if (e == -999)  break;
736                 }
737         }else{  /* t2_mode == FINAL_PASS  */
738                 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
739                 while (pi_next(&pi[pino])) {
740                         if (pi[pino].layno < maxlayers) {
741                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
742                                 if (e == -999) {
743                                         break;
744                                 } else {
745                                         c += e;
746                                 }
747                                 /* INDEX >> */
748                                 if(cstr_info) {
749                                         if(cstr_info->index_write) {
750                                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
751                                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
752                                                 if (!cstr_info->packno) {
753                                                         info_PK->start_pos = info_TL->end_header + 1;
754                                                 } else {
755                                                         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;
756                                                 }
757                                                 info_PK->end_pos = info_PK->start_pos + e - 1;
758                                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance 
759                                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet*/
760                                         }
761                                         
762                                         cstr_info->packno++;
763                                 }
764                                 /* << INDEX */
765                                 tile->packno++;
766                         }
767                 }
768         }
769         
770         pi_destroy(pi, cp, tileno);
771         
772         if (e == -999) {
773                 return e;
774         }
775         
776   return (c - dest);
777 }
778
779 opj_bool t2_encode_packets_v2(
780                                            opj_t2_v2_t* p_t2,
781                                            OPJ_UINT32 p_tile_no,
782                                            opj_tcd_tile_v2_t *p_tile,
783                                            OPJ_UINT32 p_maxlayers,
784                                            OPJ_BYTE *p_dest,
785                                            OPJ_UINT32 * p_data_written,
786                                            OPJ_UINT32 p_max_len,
787                                            opj_codestream_info_t *cstr_info,
788                                            OPJ_UINT32 p_tp_num,
789                                            OPJ_INT32 p_tp_pos,
790                                            OPJ_UINT32 p_pino,
791                                            J2K_T2_MODE p_t2_mode)
792 {
793         OPJ_BYTE *l_current_data = p_dest;
794         OPJ_UINT32 l_nb_bytes = 0;
795         OPJ_UINT32 compno;
796         OPJ_UINT32 poc;
797         opj_pi_iterator_t *l_pi = 00;
798         opj_pi_iterator_t *l_current_pi = 00;
799         opj_image_t *l_image = p_t2->image;
800         opj_cp_v2_t *l_cp = p_t2->cp;
801         opj_tcp_v2_t *l_tcp = &l_cp->tcps[p_tile_no];
802         OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == CINEMA4K_24? 2: 1;
803         OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
804         OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
805
806         l_pi = pi_initialise_encode_v2(l_image, l_cp, p_tile_no, p_t2_mode);
807         if (!l_pi) {
808                 return OPJ_FALSE;
809         }
810
811         * p_data_written = 0;
812
813         if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */
814                 l_current_pi = l_pi;
815
816                 for     (compno = 0; compno < l_max_comp; ++compno) {
817                         OPJ_UINT32 l_comp_len = 0;
818                         l_current_pi = l_pi;
819
820                         for (poc = 0; poc < pocno ; ++poc) {
821                                 OPJ_UINT32 l_tp_num = compno;
822
823                                 pi_create_encode_v2(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
824
825                                 while (pi_next(l_current_pi)) {
826                                         if (l_current_pi->layno < p_maxlayers) {
827                                                 l_nb_bytes = 0;
828
829                                                 if (! t2_encode_packet_v2(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
830                                                         pi_destroy_v2(l_pi, l_nb_pocs);
831                                                         return OPJ_FALSE;
832                                                 }
833
834                                                 l_comp_len += l_nb_bytes;
835                                                 l_current_data += l_nb_bytes;
836                                                 p_max_len -= l_nb_bytes;
837
838                                                 * p_data_written += l_nb_bytes;
839                                         }
840                                 }
841
842                                 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
843                                         if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
844                                                 pi_destroy_v2(l_pi, l_nb_pocs);
845                                                 return OPJ_FALSE;
846                                         }
847                                 }
848
849                                 ++l_current_pi;
850                         }
851                 }
852         }
853         else {  /* t2_mode == FINAL_PASS  */
854                 pi_create_encode_v2(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
855
856                 l_current_pi = &l_pi[p_pino];
857
858                 while (pi_next(l_current_pi)) {
859                         if (l_current_pi->layno < p_maxlayers) {
860                                 l_nb_bytes=0;
861
862                                 if (! t2_encode_packet_v2(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
863                                         pi_destroy_v2(l_pi, l_nb_pocs);
864                                         return OPJ_FALSE;
865                                 }
866
867                                 l_current_data += l_nb_bytes;
868                                 p_max_len -= l_nb_bytes;
869
870                                 * p_data_written += l_nb_bytes;
871
872                                 /* INDEX >> */
873                                 if(cstr_info) {
874                                         if(cstr_info->index_write) {
875                                                 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
876                                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
877                                                 if (!cstr_info->packno) {
878                                                         info_PK->start_pos = info_TL->end_header + 1;
879                                                 } else {
880                                                         info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
881                                                 }
882                                                 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
883                                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance
884                                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet
885                                         }
886
887                                         cstr_info->packno++;
888                                 }
889                                 /* << INDEX */
890                                 ++p_tile->packno;
891                         }
892                 }
893         }
894
895         pi_destroy_v2(l_pi, l_nb_pocs);
896
897         return OPJ_TRUE;
898 }
899
900
901 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) {
902         unsigned char *c = src;
903         opj_pi_iterator_t *pi;
904         int pino, e = 0;
905         int n = 0, curtp = 0;
906         int tp_start_packno;
907
908         opj_image_t *image = t2->image;
909         opj_cp_t *cp = t2->cp;
910         
911         /* create a packet iterator */
912         pi = pi_create_decode(image, cp, tileno);
913         if(!pi) {
914                 /* TODO: throw an error */
915                 return -999;
916         }
917
918         tp_start_packno = 0;
919         
920         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
921                 while (pi_next(&pi[pino])) {
922                         if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
923                                 opj_packet_info_t *pack_info;
924                                 if (cstr_info)
925                                         pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
926                                 else
927                                         pack_info = NULL;
928                                 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
929                         } else {
930                                 e = 0;
931                         }
932                         if(e == -999) return -999;
933                         /* progression in resolution */
934                         image->comps[pi[pino].compno].resno_decoded =   
935                                 (e > 0) ? 
936                                 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
937                                 : image->comps[pi[pino].compno].resno_decoded;
938                         n++;
939
940                         /* INDEX >> */
941                         if(cstr_info) {
942                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
943                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
944                                 if (!cstr_info->packno) {
945                                         info_PK->start_pos = info_TL->end_header + 1;
946                                 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ /* New tile part*/
947                                         info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part*/
948           info_TL->tp[curtp].tp_start_pack = tp_start_packno;
949                                         tp_start_packno = cstr_info->packno;
950                                         curtp++;
951                                         info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
952                                 } else {
953                                         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;
954                                 }
955                                 info_PK->end_pos = info_PK->start_pos + e - 1;
956                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance 
957                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet*/
958                                 cstr_info->packno++;
959                         }
960                         /* << INDEX */
961                         
962                         if (e == -999) {                /* ADD */
963                                 break;
964                         } else {
965                                 c += e;
966                         }                       
967                 }
968         }
969         /* INDEX >> */
970         if(cstr_info) {
971                 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part*/
972     cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno;
973         }
974         /* << INDEX */
975
976         /* don't forget to release pi */
977         pi_destroy(pi, cp, tileno);
978         
979         if (e == -999) {
980                 return e;
981         }
982         
983         return (c - src);
984 }
985
986 opj_bool t2_decode_packets_v2(
987                                                 opj_t2_v2_t *p_t2,
988                                                 OPJ_UINT32 p_tile_no,
989                                                 struct opj_tcd_tile_v2 *p_tile,
990                                                 OPJ_BYTE *p_src,
991                                                 OPJ_UINT32 * p_data_read,
992                                                 OPJ_UINT32 p_max_len,
993                                                 opj_codestream_index_t *p_cstr_index)
994 {
995         OPJ_BYTE *l_current_data = p_src;
996         opj_pi_iterator_t *l_pi = 00;
997         OPJ_UINT32 pino;
998         opj_image_t *l_image = p_t2->image;
999         opj_cp_v2_t *l_cp = p_t2->cp;
1000         opj_cp_v2_t *cp = p_t2->cp;
1001         opj_tcp_v2_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
1002         OPJ_UINT32 l_nb_bytes_read;
1003         OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
1004         opj_pi_iterator_t *l_current_pi = 00;
1005         OPJ_UINT32 curtp = 0;
1006         OPJ_UINT32 tp_start_packno;
1007         opj_packet_info_t *l_pack_info = 00;
1008         opj_image_comp_t* l_img_comp = 00;
1009
1010 #ifdef TODO_MSD
1011         if (p_cstr_index) {
1012                 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
1013         }
1014 #endif
1015
1016         /* create a packet iterator */
1017         l_pi = pi_create_decode_v2(l_image, l_cp, p_tile_no);
1018         if (!l_pi) {
1019                 return OPJ_FALSE;
1020         }
1021
1022         tp_start_packno = 0;
1023         l_current_pi = l_pi;
1024
1025         for     (pino = 0; pino <= l_tcp->numpocs; ++pino) {
1026
1027                 /* if the resolution needed is to low, one dim of the tilec could be equal to zero
1028                  * and no packets are used to encode this resolution and
1029                  * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
1030                  * and no l_img_comp->resno_decoded are computed
1031                  */
1032                 opj_bool* first_pass_failed = (opj_bool*)opj_malloc(l_image->numcomps * sizeof(opj_bool));
1033                 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(opj_bool));
1034
1035                 while (pi_next(l_current_pi)) {
1036
1037
1038                         if (l_tcp->num_layers_to_decode > l_current_pi->layno
1039                                         && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
1040                                 l_nb_bytes_read = 0;
1041
1042                                 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
1043
1044                                 if (! 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)) {
1045                                         pi_destroy_v2(l_pi,l_nb_pocs);
1046                                         return OPJ_FALSE;
1047                                 }
1048
1049                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
1050                                 l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
1051                         }
1052                         else {
1053                                 l_nb_bytes_read = 0;
1054                                 if (! 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)) {
1055                                         pi_destroy_v2(l_pi,l_nb_pocs);
1056                                         return OPJ_FALSE;
1057                                 }
1058                         }
1059
1060                         if (first_pass_failed[l_current_pi->compno]) {
1061                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
1062                                 if (l_img_comp->resno_decoded == 0)
1063                                         l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
1064                         }
1065
1066                         l_current_data += l_nb_bytes_read;
1067                         p_max_len -= l_nb_bytes_read;
1068
1069                         /* INDEX >> */
1070 #ifdef TODO_MSD
1071                         if(p_cstr_info) {
1072                                 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
1073                                 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
1074                                 if (!p_cstr_info->packno) {
1075                                         info_PK->start_pos = info_TL->end_header + 1;
1076                                 } 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 */
1077                                         info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
1078                                         tp_start_packno = p_cstr_info->packno;
1079                                         curtp++;
1080                                         info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
1081                                 } else {
1082                                         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;
1083                                 }
1084                                 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
1085                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance */
1086                                 ++p_cstr_info->packno;
1087                         }
1088 #endif
1089                         /* << INDEX */
1090                 }
1091                 ++l_current_pi;
1092
1093                 opj_free(first_pass_failed);
1094         }
1095         /* INDEX >> */
1096 #ifdef TODO_MSD
1097         if
1098                 (p_cstr_info) {
1099                 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 */
1100         }
1101 #endif
1102         /* << INDEX */
1103
1104         /* don't forget to release pi */
1105         pi_destroy_v2(l_pi,l_nb_pocs);
1106         *p_data_read = l_current_data - p_src;
1107         return OPJ_TRUE;
1108 }
1109
1110 /* ----------------------------------------------------------------------- */
1111
1112 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
1113         /* create the tcd structure */
1114         opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
1115         if(!t2) return NULL;
1116         t2->cinfo = cinfo;
1117         t2->image = image;
1118         t2->cp = cp;
1119
1120         return t2;
1121 }
1122
1123 /**
1124  * Creates a Tier 2 handle
1125  *
1126  * @param       p_image         Source or destination image
1127  * @param       p_cp            Image coding parameters.
1128  * @return              a new T2 handle if successful, NULL otherwise.
1129 */
1130 opj_t2_v2_t* t2_create_v2(      opj_image_t *p_image,
1131                                                         opj_cp_v2_t *p_cp)
1132 {
1133         /* create the tcd structure */
1134         opj_t2_v2_t *l_t2 = (opj_t2_v2_t*)opj_malloc(sizeof(opj_t2_v2_t));
1135         if (!l_t2) {
1136                 return NULL;
1137         }
1138         memset(l_t2,0,sizeof(opj_t2_v2_t));
1139
1140         l_t2->image = p_image;
1141         l_t2->cp = p_cp;
1142
1143         return l_t2;
1144 }
1145
1146 void t2_destroy(opj_t2_t *t2) {
1147         if(t2) {
1148                 opj_free(t2);
1149         }
1150 }
1151
1152 void t2_destroy_v2(opj_t2_v2_t *t2) {
1153         if(t2) {
1154                 opj_free(t2);
1155         }
1156 }
1157
1158
1159 static opj_bool t2_decode_packet_v2(
1160                                                          opj_t2_v2_t* p_t2,
1161                                                          opj_tcd_tile_v2_t *p_tile,
1162                              opj_tcp_v2_t *p_tcp,
1163                                                          opj_pi_iterator_t *p_pi,
1164                                                          OPJ_BYTE *p_src,
1165                                                          OPJ_UINT32 * p_data_read,
1166                                                          OPJ_UINT32 p_max_length,
1167                                                          opj_packet_info_t *p_pack_info)
1168 {
1169         opj_bool l_read_data;
1170         OPJ_UINT32 l_nb_bytes_read = 0;
1171         OPJ_UINT32 l_nb_total_bytes_read = 0;
1172
1173         *p_data_read = 0;
1174
1175         if (! 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)) {
1176                 return OPJ_FALSE;
1177         }
1178
1179         p_src += l_nb_bytes_read;
1180         l_nb_total_bytes_read += l_nb_bytes_read;
1181         p_max_length -= l_nb_bytes_read;
1182
1183         /* we should read data for the packet */
1184         if (l_read_data) {
1185                 l_nb_bytes_read = 0;
1186
1187                 if (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
1188                         return OPJ_FALSE;
1189                 }
1190
1191                 l_nb_total_bytes_read += l_nb_bytes_read;
1192         }
1193
1194         *p_data_read = l_nb_total_bytes_read;
1195
1196         return OPJ_TRUE;
1197 }
1198
1199 static opj_bool t2_encode_packet_v2(
1200                                                          OPJ_UINT32 tileno,
1201                                                          opj_tcd_tile_v2_t * tile,
1202                                                          opj_tcp_v2_t * tcp,
1203                                                          opj_pi_iterator_t *pi,
1204                                                          OPJ_BYTE *dest,
1205                                                          OPJ_UINT32 * p_data_written,
1206                                                          OPJ_UINT32 length,
1207                                                          opj_codestream_info_t *cstr_info)
1208 {
1209         OPJ_UINT32 bandno, cblkno;
1210         OPJ_BYTE *c = dest;
1211         OPJ_UINT32 l_nb_bytes;
1212         OPJ_UINT32 compno = pi->compno; /* component value */
1213         OPJ_UINT32 resno  = pi->resno;          /* resolution level value */
1214         OPJ_UINT32 precno = pi->precno; /* precinct value */
1215         OPJ_UINT32 layno  = pi->layno;          /* quality layer value */
1216         OPJ_UINT32 l_nb_blocks;
1217         opj_tcd_band_v2_t *band = 00;
1218         opj_tcd_cblk_enc_v2_t* cblk = 00;
1219         opj_tcd_pass_v2_t *pass = 00;
1220
1221         opj_tcd_tilecomp_v2_t *tilec = &tile->comps[compno];
1222         opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1223
1224         opj_bio_t *bio = 00;    /* BIO component */
1225
1226         /* <SOP 0xff91> */
1227         if (tcp->csty & J2K_CP_CSTY_SOP) {
1228                 c[0] = 255;
1229                 c[1] = 145;
1230                 c[2] = 0;
1231                 c[3] = 4;
1232                 c[4] = (tile->packno % 65536) / 256;
1233                 c[5] = (tile->packno % 65536) % 256;
1234                 c += 6;
1235                 length -= 6;
1236         }
1237         /* </SOP> */
1238
1239         if (!layno) {
1240                 band = res->bands;
1241
1242                 for(bandno = 0; bandno < res->numbands; ++bandno) {
1243                         opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1244
1245                         tgt_reset(prc->incltree);
1246                         tgt_reset(prc->imsbtree);
1247
1248                         l_nb_blocks = prc->cw * prc->ch;
1249                         for     (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
1250                                 opj_tcd_cblk_enc_v2_t* cblk = &prc->cblks.enc[cblkno];
1251
1252                                 cblk->numpasses = 0;
1253                                 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
1254                         }
1255                         ++band;
1256                 }
1257         }
1258
1259         bio = bio_create();
1260         bio_init_enc(bio, c, length);
1261         bio_write(bio, 1, 1);           /* Empty header bit */
1262
1263         /* Writing Packet header */
1264         band = res->bands;
1265         for (bandno = 0; bandno < res->numbands; ++bandno)      {
1266                 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1267
1268                 l_nb_blocks = prc->cw * prc->ch;
1269                 cblk = prc->cblks.enc;
1270
1271                 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
1272                         opj_tcd_layer_t *layer = &cblk->layers[layno];
1273
1274                         if (!cblk->numpasses && layer->numpasses) {
1275                                 tgt_setvalue(prc->incltree, cblkno, layno);
1276                         }
1277
1278                         ++cblk;
1279                 }
1280
1281                 cblk = prc->cblks.enc;
1282                 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
1283                         opj_tcd_layer_t *layer = &cblk->layers[layno];
1284                         OPJ_UINT32 increment = 0;
1285                         OPJ_UINT32 nump = 0;
1286                         OPJ_UINT32 len = 0, passno;
1287                         OPJ_UINT32 l_nb_passes;
1288
1289                         /* cblk inclusion bits */
1290                         if (!cblk->numpasses) {
1291                                 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
1292                         } else {
1293                                 bio_write(bio, layer->numpasses != 0, 1);
1294                         }
1295
1296                         /* if cblk not included, go to the next cblk  */
1297                         if (!layer->numpasses) {
1298                                 ++cblk;
1299                                 continue;
1300                         }
1301
1302                         /* if first instance of cblk --> zero bit-planes information */
1303                         if (!cblk->numpasses) {
1304                                 cblk->numlenbits = 3;
1305                                 tgt_encode(bio, prc->imsbtree, cblkno, 999);
1306                         }
1307
1308                         /* number of coding passes included */
1309                         t2_putnumpasses(bio, layer->numpasses);
1310                         l_nb_passes = cblk->numpasses + layer->numpasses;
1311                         pass = cblk->passes +  cblk->numpasses;
1312
1313                         /* computation of the increase of the length indicator and insertion in the header     */
1314                         for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
1315                                 ++nump;
1316                                 len += pass->len;
1317
1318                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
1319                                         increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
1320                                         len = 0;
1321                                         nump = 0;
1322                                 }
1323
1324                                 ++pass;
1325                         }
1326                         t2_putcommacode(bio, increment);
1327
1328                         /* computation of the new Length indicator */
1329                         cblk->numlenbits += increment;
1330
1331                         pass = cblk->passes +  cblk->numpasses;
1332                         /* insertion of the codeword segment length */
1333                         for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
1334                                 nump++;
1335                                 len += pass->len;
1336
1337                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
1338                                         bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
1339                                         len = 0;
1340                                         nump = 0;
1341                                 }
1342                                 ++pass;
1343                         }
1344
1345                         ++cblk;
1346                 }
1347
1348                 ++band;
1349         }
1350
1351         if (bio_flush(bio)) {
1352                 bio_destroy(bio);
1353                 return OPJ_FALSE;               /* modified to eliminate longjmp !! */
1354         }
1355
1356         l_nb_bytes = bio_numbytes(bio);
1357         c += l_nb_bytes;
1358         length -= l_nb_bytes;
1359
1360         bio_destroy(bio);
1361
1362         /* <EPH 0xff92> */
1363         if (tcp->csty & J2K_CP_CSTY_EPH) {
1364                 c[0] = 255;
1365                 c[1] = 146;
1366                 c += 2;
1367                 length -= 2;
1368         }
1369         /* </EPH> */
1370
1371         /* << INDEX */
1372         // End of packet header position. Currently only represents the distance to start of packet
1373         // Will be updated later by incrementing with packet start value
1374         if(cstr_info && cstr_info->index_write) {
1375                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
1376                 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
1377         }
1378         /* INDEX >> */
1379
1380         /* Writing the packet body */
1381         band = res->bands;
1382         for (bandno = 0; bandno < res->numbands; bandno++) {
1383                 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1384
1385                 l_nb_blocks = prc->cw * prc->ch;
1386                 cblk = prc->cblks.enc;
1387
1388                 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
1389                         opj_tcd_layer_t *layer = &cblk->layers[layno];
1390
1391                         if (!layer->numpasses) {
1392                                 ++cblk;
1393                                 continue;
1394                         }
1395
1396                         if (layer->len > length) {
1397                                 return OPJ_FALSE;
1398                         }
1399
1400                         memcpy(c, layer->data, layer->len);
1401                         cblk->numpasses += layer->numpasses;
1402                         c += layer->len;
1403                         length -= layer->len;
1404
1405                         /* << INDEX */
1406                         if(cstr_info && cstr_info->index_write) {
1407                                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
1408                                 info_PK->disto += layer->disto;
1409                                 if (cstr_info->D_max < info_PK->disto) {
1410                                         cstr_info->D_max = info_PK->disto;
1411                                 }
1412                         }
1413
1414                         ++cblk;
1415                         /* INDEX >> */
1416                 }
1417                 ++band;
1418         }
1419
1420         * p_data_written += (c - dest);
1421
1422         return OPJ_TRUE;
1423 }
1424
1425 static opj_bool t2_skip_packet(
1426                                                          opj_t2_v2_t* p_t2,
1427                                                          opj_tcd_tile_v2_t *p_tile,
1428                              opj_tcp_v2_t *p_tcp,
1429                                                          opj_pi_iterator_t *p_pi,
1430                                                          OPJ_BYTE *p_src,
1431                                                          OPJ_UINT32 * p_data_read,
1432                                                          OPJ_UINT32 p_max_length,
1433                                                          opj_packet_info_t *p_pack_info)
1434 {
1435         opj_bool l_read_data;
1436         OPJ_UINT32 l_nb_bytes_read = 0;
1437         OPJ_UINT32 l_nb_total_bytes_read = 0;
1438
1439         *p_data_read = 0;
1440
1441         if (! 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)) {
1442                 return OPJ_FALSE;
1443         }
1444
1445         p_src += l_nb_bytes_read;
1446         l_nb_total_bytes_read += l_nb_bytes_read;
1447         p_max_length -= l_nb_bytes_read;
1448
1449         /* we should read data for the packet */
1450         if (l_read_data) {
1451                 l_nb_bytes_read = 0;
1452
1453                 if (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
1454                         return OPJ_FALSE;
1455                 }
1456
1457                 l_nb_total_bytes_read += l_nb_bytes_read;
1458         }
1459         *p_data_read = l_nb_total_bytes_read;
1460
1461         return OPJ_TRUE;
1462 }
1463
1464
1465
1466 static opj_bool t2_read_packet_header(
1467                                                          opj_t2_v2_t* p_t2,
1468                                                          opj_tcd_tile_v2_t *p_tile,
1469                              opj_tcp_v2_t *p_tcp,
1470                                                          opj_pi_iterator_t *p_pi,
1471                                                          opj_bool * p_is_data_present,
1472                                                          OPJ_BYTE *p_src_data,
1473                                                          OPJ_UINT32 * p_data_read,
1474                                                          OPJ_UINT32 p_max_length,
1475                                                          opj_packet_info_t *p_pack_info)
1476 {
1477         /* loop */
1478         OPJ_UINT32 bandno, cblkno;
1479         OPJ_UINT32 l_nb_code_blocks;
1480         OPJ_UINT32 l_remaining_length;
1481         OPJ_UINT32 l_header_length;
1482         OPJ_UINT32 * l_modified_length_ptr = 00;
1483         OPJ_BYTE *l_current_data = p_src_data;
1484         opj_cp_v2_t *l_cp = p_t2->cp;
1485         opj_bio_t *l_bio = 00;  /* BIO component */
1486         opj_tcd_band_v2_t *l_band = 00;
1487         opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1488         opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1489
1490         OPJ_BYTE *l_header_data = 00;
1491         OPJ_BYTE **l_header_data_start = 00;
1492
1493         OPJ_UINT32 l_present;
1494
1495         if (p_pi->layno == 0) {
1496                 l_band = l_res->bands;
1497
1498                 /* reset tagtrees */
1499                 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1500                         opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1501
1502                         if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
1503                                 tgt_reset(l_prc->incltree);
1504                                 tgt_reset(l_prc->imsbtree);
1505                                 l_cblk = l_prc->cblks.dec;
1506
1507                                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1508                                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1509                                         l_cblk->numsegs = 0;
1510                                         l_cblk->real_num_segs = 0;
1511                                         ++l_cblk;
1512                                 }
1513                         }
1514
1515                         ++l_band;
1516                 }
1517         }
1518
1519         /* SOP markers */
1520
1521         if (p_tcp->csty & J2K_CP_CSTY_SOP) {
1522                 if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
1523                         /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */
1524                 } else {
1525                         l_current_data += 6;
1526                 }
1527
1528                 /** TODO : check the Nsop value */
1529         }
1530
1531         /*
1532         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
1533         This part deal with this caracteristic
1534         step 1: Read packet header in the saved structure
1535         step 2: Return to codestream for decoding
1536         */
1537
1538         l_bio = bio_create();
1539         if (! l_bio) {
1540                 return OPJ_FALSE;
1541         }
1542
1543         if (l_cp->ppm == 1) { /* PPM */
1544                 l_header_data_start = &l_cp->ppm_data;
1545                 l_header_data = *l_header_data_start;
1546                 l_modified_length_ptr = &(l_cp->ppm_len);
1547
1548         }
1549         else if (p_tcp->ppt == 1) { /* PPT */
1550                 l_header_data_start = &(p_tcp->ppt_data);
1551                 l_header_data = *l_header_data_start;
1552                 l_modified_length_ptr = &(p_tcp->ppt_len);
1553         }
1554         else {  /* Normal Case */
1555                 l_header_data_start = &(l_current_data);
1556                 l_header_data = *l_header_data_start;
1557                 l_remaining_length = p_src_data+p_max_length-l_header_data;
1558                 l_modified_length_ptr = &(l_remaining_length);
1559         }
1560
1561         bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
1562
1563         l_present = bio_read(l_bio, 1);
1564         if (!l_present) {
1565                 bio_inalign(l_bio);
1566                 l_header_data += bio_numbytes(l_bio);
1567                 bio_destroy(l_bio);
1568
1569                 /* EPH markers */
1570                 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1571                         if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1572                                 printf("Error : expected EPH marker\n");
1573                         } else {
1574                                 l_header_data += 2;
1575                         }
1576                 }
1577
1578                 l_header_length = (l_header_data - *l_header_data_start);
1579                 *l_modified_length_ptr -= l_header_length;
1580                 *l_header_data_start += l_header_length;
1581
1582                 /* << INDEX */
1583                 /* End of packet header position. Currently only represents the distance to start of packet
1584                    Will be updated later by incrementing with packet start value */
1585                 if (p_pack_info) {
1586                         p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1587                 }
1588                 /* INDEX >> */
1589
1590                 * p_is_data_present = OPJ_FALSE;
1591                 *p_data_read = l_current_data - p_src_data;
1592                 return OPJ_TRUE;
1593         }
1594
1595         l_band = l_res->bands;
1596         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1597                 opj_tcd_precinct_v2_t *l_prc = &(l_band->precincts[p_pi->precno]);
1598
1599                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1600                         ++l_band;
1601                         continue;
1602                 }
1603
1604                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1605                 l_cblk = l_prc->cblks.dec;
1606                 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
1607                         OPJ_UINT32 l_included,l_increment, l_segno;
1608                         OPJ_INT32 n;
1609
1610                         /* if cblk not yet included before --> inclusion tagtree */
1611                         if (!l_cblk->numsegs) {
1612                                 l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1);
1613                                 /* else one bit */
1614                         }
1615                         else {
1616                                 l_included = bio_read(l_bio, 1);
1617                         }
1618
1619                         /* if cblk not included */
1620                         if (!l_included) {
1621                                 l_cblk->numnewpasses = 0;
1622                                 ++l_cblk;
1623                                 continue;
1624                         }
1625
1626                         /* if cblk not yet included --> zero-bitplane tagtree */
1627                         if (!l_cblk->numsegs) {
1628                                 OPJ_UINT32 i = 0;
1629
1630                                 while (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i)) {
1631                                         ++i;
1632                                 }
1633
1634                                 l_cblk->numbps = l_band->numbps + 1 - i;
1635                                 l_cblk->numlenbits = 3;
1636                         }
1637
1638                         /* number of coding passes */
1639                         l_cblk->numnewpasses = t2_getnumpasses(l_bio);
1640                         l_increment = t2_getcommacode(l_bio);
1641
1642                         /* length indicator increment */
1643                         l_cblk->numlenbits += l_increment;
1644                         l_segno = 0;
1645
1646                         if (!l_cblk->numsegs) {
1647                                 if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1648                                         bio_destroy(l_bio);
1649                                         return OPJ_FALSE;
1650                                 }
1651                         }
1652                         else {
1653                                 l_segno = l_cblk->numsegs - 1;
1654                                 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1655                                         ++l_segno;
1656                                         if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1657                                                 bio_destroy(l_bio);
1658                                                 return OPJ_FALSE;
1659                                         }
1660                                 }
1661                         }
1662                         n = l_cblk->numnewpasses;
1663
1664                         do {
1665                                 l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n);
1666                                 l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1667
1668                                 n -= l_cblk->segs[l_segno].numnewpasses;
1669                                 if (n > 0) {
1670                                         ++l_segno;
1671
1672                                         if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1673                                                 bio_destroy(l_bio);
1674                                                 return OPJ_FALSE;
1675                                         }
1676                                 }
1677                         } while (n > 0);
1678
1679                         ++l_cblk;
1680                 }
1681
1682                 ++l_band;
1683         }
1684
1685         if (bio_inalign(l_bio)) {
1686                 bio_destroy(l_bio);
1687                 return OPJ_FALSE;
1688         }
1689
1690         l_header_data += bio_numbytes(l_bio);
1691         bio_destroy(l_bio);
1692
1693         /* EPH markers */
1694         if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1695                 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1696                         /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
1697                 } else {
1698                         l_header_data += 2;
1699                 }
1700         }
1701
1702         l_header_length = (l_header_data - *l_header_data_start);
1703         *l_modified_length_ptr -= l_header_length;
1704         *l_header_data_start += l_header_length;
1705
1706         /* << INDEX */
1707         /* End of packet header position. Currently only represents the distance to start of packet
1708          Will be updated later by incrementing with packet start value */
1709         if (p_pack_info) {
1710                 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1711         }
1712         /* INDEX >> */
1713
1714         *p_is_data_present = OPJ_TRUE;
1715         *p_data_read = l_current_data - p_src_data;
1716
1717         return OPJ_TRUE;
1718 }
1719
1720 static opj_bool t2_read_packet_data(
1721                                                          opj_t2_v2_t* p_t2,
1722                                                          opj_tcd_tile_v2_t *p_tile,
1723                                                          opj_pi_iterator_t *p_pi,
1724                                                          OPJ_BYTE *p_src_data,
1725                                                          OPJ_UINT32 * p_data_read,
1726                                                          OPJ_UINT32 p_max_length,
1727                                                          opj_packet_info_t *pack_info)
1728 {
1729         OPJ_UINT32 bandno, cblkno;
1730         OPJ_UINT32 l_nb_code_blocks;
1731         OPJ_BYTE *l_current_data = p_src_data;
1732         opj_tcd_band_v2_t *l_band = 00;
1733         opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1734         opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1735
1736         l_band = l_res->bands;
1737         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1738                 opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1739
1740                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1741                         ++l_band;
1742                         continue;
1743                 }
1744
1745                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1746                 l_cblk = l_prc->cblks.dec;
1747
1748                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1749                         opj_tcd_seg_t *l_seg = 00;
1750
1751                         if (!l_cblk->numnewpasses) {
1752                                 /* nothing to do */
1753                                 ++l_cblk;
1754                                 continue;
1755                         }
1756
1757                         if (!l_cblk->numsegs) {
1758                                 l_seg = l_cblk->segs;
1759                                 ++l_cblk->numsegs;
1760                                 l_cblk->len = 0;
1761                         }
1762                         else {
1763                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1764
1765                                 if (l_seg->numpasses == l_seg->maxpasses) {
1766                                         ++l_seg;
1767                                         ++l_cblk->numsegs;
1768                                 }
1769                         }
1770
1771                         do {
1772                                 if (l_current_data + l_seg->newlen > p_src_data + p_max_length) {
1773                                         return OPJ_FALSE;
1774                                 }
1775
1776 #ifdef USE_JPWL
1777                         /* we need here a j2k handle to verify if making a check to
1778                         the validity of cblocks parameters is selected from user (-W) */
1779
1780                                 /* let's check that we are not exceeding */
1781                                 if ((l_cblk->len + l_seg->newlen) > 8192) {
1782                                         opj_event_msg(p_t2->cinfo, EVT_WARNING,
1783                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1784                                                 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1785                                         if (!JPWL_ASSUME) {
1786                                                 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1787                                                 return OPJ_FALSE;
1788                                         }
1789                                         l_seg->newlen = 8192 - l_cblk->len;
1790                                         opj_event_msg(p_t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", l_seg->newlen);
1791                                         break;
1792                                 };
1793
1794 #endif /* USE_JPWL */
1795
1796                                 memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen);
1797
1798                                 if (l_seg->numpasses == 0) {
1799                                         l_seg->data = &l_cblk->data;
1800                                         l_seg->dataindex = l_cblk->len;
1801                                 }
1802
1803                                 l_current_data += l_seg->newlen;
1804                                 l_seg->numpasses += l_seg->numnewpasses;
1805                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1806
1807                                 l_seg->real_num_passes = l_seg->numpasses;
1808                                 l_cblk->len += l_seg->newlen;
1809                                 l_seg->len += l_seg->newlen;
1810
1811                                 if (l_cblk->numnewpasses > 0) {
1812                                         ++l_seg;
1813                                         ++l_cblk->numsegs;
1814                                 }
1815                         } while (l_cblk->numnewpasses > 0);
1816
1817                         l_cblk->real_num_segs = l_cblk->numsegs;
1818                         ++l_cblk;
1819                 }
1820
1821                 ++l_band;
1822         }
1823
1824         *(p_data_read) = l_current_data - p_src_data;
1825
1826         return OPJ_TRUE;
1827 }
1828
1829 static opj_bool t2_skip_packet_data(
1830                                                          opj_t2_v2_t* p_t2,
1831                                                          opj_tcd_tile_v2_t *p_tile,
1832                                                          opj_pi_iterator_t *p_pi,
1833                                                          OPJ_UINT32 * p_data_read,
1834                                                          OPJ_UINT32 p_max_length,
1835                                                          opj_packet_info_t *pack_info)
1836 {
1837         OPJ_UINT32 bandno, cblkno;
1838         OPJ_UINT32 l_nb_code_blocks;
1839         opj_tcd_band_v2_t *l_band = 00;
1840         opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1841         opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1842
1843         *p_data_read = 0;
1844         l_band = l_res->bands;
1845
1846         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1847                 opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1848
1849                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1850                         ++l_band;
1851                         continue;
1852                 }
1853
1854                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1855                 l_cblk = l_prc->cblks.dec;
1856
1857                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1858                         opj_tcd_seg_t *l_seg = 00;
1859
1860                         if (!l_cblk->numnewpasses) {
1861                                 /* nothing to do */
1862                                 ++l_cblk;
1863                                 continue;
1864                         }
1865
1866                         if (!l_cblk->numsegs) {
1867                                 l_seg = l_cblk->segs;
1868                                 ++l_cblk->numsegs;
1869                                 l_cblk->len = 0;
1870                         }
1871                         else {
1872                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1873
1874                                 if (l_seg->numpasses == l_seg->maxpasses) {
1875                                         ++l_seg;
1876                                         ++l_cblk->numsegs;
1877                                 }
1878                         }
1879
1880                         do {
1881                                 if (* p_data_read + l_seg->newlen > p_max_length) {
1882                                         return OPJ_FALSE;
1883                                 }
1884
1885 #ifdef USE_JPWL
1886                         /* we need here a j2k handle to verify if making a check to
1887                         the validity of cblocks parameters is selected from user (-W) */
1888
1889                                 /* let's check that we are not exceeding */
1890                                 if ((l_cblk->len + l_seg->newlen) > 8192) {
1891                                         opj_event_msg(p_t2->cinfo, EVT_WARNING,
1892                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1893                                                 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1894                                         if (!JPWL_ASSUME) {
1895                                                 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1896                                                 return -999;
1897                                         }
1898                                         l_seg->newlen = 8192 - l_cblk->len;
1899                                         opj_event_msg(p_t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", l_seg->newlen);
1900                                         break;
1901                                 };
1902
1903 #endif /* USE_JPWL */
1904                                 *(p_data_read) += l_seg->newlen;
1905
1906                                 l_seg->numpasses += l_seg->numnewpasses;
1907                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1908                                 if (l_cblk->numnewpasses > 0)
1909                                 {
1910                                         ++l_seg;
1911                                         ++l_cblk->numsegs;
1912                                 }
1913                         } while (l_cblk->numnewpasses > 0);
1914
1915                         ++l_cblk;
1916                 }
1917
1918                 ++l_band;
1919         }
1920
1921         return OPJ_TRUE;
1922 }
1923
1924
1925 static opj_bool t2_init_seg_v2(opj_tcd_cblk_dec_v2_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first)
1926 {
1927         opj_tcd_seg_t* seg = 00;
1928         OPJ_UINT32 l_nb_segs = index + 1;
1929
1930         if (l_nb_segs > cblk->m_current_max_segs) {
1931                 cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS;
1932
1933                 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1934                 if(! cblk->segs) {
1935                         return OPJ_FALSE;
1936                 }
1937         }
1938
1939         seg = &cblk->segs[index];
1940         memset(seg,0,sizeof(opj_tcd_seg_t));
1941
1942         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1943                 seg->maxpasses = 1;
1944         }
1945         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1946                 if (first) {
1947                         seg->maxpasses = 10;
1948                 } else {
1949                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1950                 }
1951         } else {
1952                 seg->maxpasses = 109;
1953         }
1954
1955         return OPJ_TRUE;
1956 }