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