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