0830c29271d83ac552944b9a059eff53328456ed
[openjpeg.git] / src / lib / openjp3d / t2.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2001-2003, David Janssens
8  * Copyright (c) 2002-2003, Yannick Verschueren
9  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10  * Copyright (c) 2005, Herve Drolon, FreeImage Team
11  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include "opj_includes.h"
37
38 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
39 /*@{*/
40
41 /** @name Local static functions */
42 /*@{*/
43
44 static void t2_putcommacode(opj_bio_t *bio, int n);
45 static int t2_getcommacode(opj_bio_t *bio);
46 /**
47 Variable length code for signalling delta Zil (truncation point)
48 @param bio Bit Input/Output component
49 @param n delta Zil
50 */
51 static void t2_putnumpasses(opj_bio_t *bio, int n);
52 static int t2_getnumpasses(opj_bio_t *bio);
53 /**
54 Encode a packet of a tile to a destination buffer
55 @param tile Tile for which to write the packets
56 @param tcp Tile coding parameters
57 @param pi Packet identity
58 @param dest Destination buffer
59 @param len Length of the destination buffer
60 @param volume_info Structure to create an index file
61 @param tileno Number of the tile encoded
62 @param cp Coding parameters
63 @return Number of bytes encoded from the packet
64 */
65 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_volume_info_t *volume_info, int tileno, opj_cp_t *cp);
66 /**
67 Initialize the segment decoder
68 @param seg Segment instance
69 @param cblksty Codeblock style
70 @param first Is first segment
71 */
72 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
73 /**
74 Decode a packet of a tile from a source buffer
75 @param t2 T2 handle
76 @param src Source buffer
77 @param len Length of the source buffer
78 @param tile Tile for which to write the packets
79 @param tcp Tile coding parameters
80 @param pi Packet identity
81 @return Number of bytes decoded from the packet
82 */
83 int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi);
84
85 /*@}*/
86
87 /*@}*/
88
89 /* ----------------------------------------------------------------------- */
90
91 /* #define RESTART 0x04 */
92 static void t2_putcommacode(opj_bio_t *bio, int n) {
93         while (--n >= 0) {
94                 bio_write(bio, 1, 1);
95         }
96         bio_write(bio, 0, 1);
97 }
98
99 static int t2_getcommacode(opj_bio_t *bio) {
100         int n;
101         for (n = 0; bio_read(bio, 1); n++) {
102                 ;
103         }
104         return n;
105 }
106
107 static void t2_putnumpasses(opj_bio_t *bio, int n) {
108         if (n == 1) {
109                 bio_write(bio, 0, 1);
110         } else if (n == 2) {
111                 bio_write(bio, 2, 2);
112         } else if (n <= 5) {
113                 bio_write(bio, 0xc | (n - 3), 4);
114         } else if (n <= 36) {
115                 bio_write(bio, 0x1e0 | (n - 6), 9);
116         } else if (n <= 164) {
117                 bio_write(bio, 0xff80 | (n - 37), 16);
118         }
119 }
120
121 static int t2_getnumpasses(opj_bio_t *bio) {
122         int n;
123         if (!bio_read(bio, 1))
124                 return 1;
125         if (!bio_read(bio, 1))
126                 return 2;
127         if ((n = bio_read(bio, 2)) != 3)
128                 return (3 + n);
129         if ((n = bio_read(bio, 5)) != 31)
130                 return (6 + n);
131         return (37 + bio_read(bio, 7));
132 }
133
134 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_volume_info_t * volume_info, int tileno, opj_cp_t *cp) {
135         int bandno, cblkno;
136         unsigned char *sop = 0, *eph = 0;
137         unsigned char *c = dest;
138
139         int compno = pi->compno;        /* component value */
140         int resno  = pi->resno;         /* resolution level value */
141         int precno = pi->precno;        /* precinct value */
142         int layno  = pi->layno;         /* quality layer value */
143
144         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
145         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
146         
147         opj_bio_t *bio = NULL;  /* BIO component */
148
149         /* <SOP 0xff91> */
150         if ((tcp->csty & J3D_CP_CSTY_SOP)) {
151                 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
152                 sop[0] = 255;
153                 sop[1] = 145;
154                 sop[2] = 0;
155                 sop[3] = 4;
156                 sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ;
157                 sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 65536) % 256 ;
158                 memcpy(c, sop, 6);
159                 opj_free(sop);
160                 c += 6;
161         } 
162         /* </SOP> */
163         
164         if (!layno) {
165                 for (bandno = 0; bandno < res->numbands; bandno++) {
166                         opj_tcd_band_t *band = &res->bands[bandno];
167                         opj_tcd_precinct_t *prc = &band->precincts[precno];
168                         tgt_reset(prc->incltree);
169                         tgt_reset(prc->imsbtree);
170                         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
171                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
172                                 cblk->numpasses = 0;
173                 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
174                         }
175                 }
176         }
177                 
178         bio = bio_create();
179         bio_init_enc(bio, c, len);
180         bio_write(bio, 1, 1);           /* Empty header bit */
181         
182         /* Writing Packet header */
183         for (bandno = 0; bandno < res->numbands; bandno++) {
184                 opj_tcd_band_t *band = &res->bands[bandno];
185                 opj_tcd_precinct_t *prc = &band->precincts[precno];
186                 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
187                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
188                         opj_tcd_layer_t *layer = &cblk->layers[layno];
189                         if (!cblk->numpasses && layer->numpasses) {
190                 tgt_setvalue(prc->incltree, cblkno, layno);
191                         }
192                 }
193
194                 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
195                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
196                         opj_tcd_layer_t *layer = &cblk->layers[layno];
197                         int increment = 0;
198                         int nump = 0;
199                         int len = 0, passno;
200                         /* cblk inclusion bits */
201                         if (!cblk->numpasses) {
202                                 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
203                         } else {
204                                 bio_write(bio, layer->numpasses != 0, 1);
205                         }
206                         /* if cblk not included, go to the next cblk  */
207                         if (!layer->numpasses) {
208                                 continue;
209                         }
210                         /* if first instance of cblk --> zero bit-planes information */
211                         if (!cblk->numpasses) {
212                                 cblk->numlenbits = 3;
213                                 tgt_encode(bio, prc->imsbtree, cblkno, 999);
214                         }
215                         /* number of coding passes included */
216                         t2_putnumpasses(bio, layer->numpasses);
217                         
218                         /* computation of the increase of the length indicator and insertion in the header     */
219                         for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
220                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
221                                 nump++;
222                                 len += pass->len;
223                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
224                                         increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
225                                         len = 0;
226                                         nump = 0;
227                                 }
228                         }
229                         t2_putcommacode(bio, increment);
230
231                         /* computation of the new Length indicator */
232                         cblk->numlenbits += increment;
233
234                         /* insertion of the codeword segment length */
235                         for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
236                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
237                                 nump++;
238                                 len += pass->len;
239                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
240                                         bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
241                                         len = 0;
242                                         nump = 0;
243                                 }
244                         }
245
246                 }
247         }
248         
249         
250         if (bio_flush(bio)) {
251                 return -999;            /* modified to eliminate longjmp !! */
252         }
253         
254         c += bio_numbytes(bio);
255
256         bio_destroy(bio);
257         
258         /* <EPH 0xff92> */
259         if (tcp->csty & J3D_CP_CSTY_EPH) {
260                 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
261                 eph[0] = 255;
262                 eph[1] = 146;
263                 memcpy(c, eph, 2);
264                 opj_free(eph);
265                 c += 2;
266         }
267         /* </EPH> */
268         
269         /* Writing the packet body */
270         
271         for (bandno = 0; bandno < res->numbands; bandno++) {
272                 opj_tcd_band_t *band = &res->bands[bandno];
273                 opj_tcd_precinct_t *prc = &band->precincts[precno];
274                 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
275                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
276                         opj_tcd_layer_t *layer = &cblk->layers[layno];
277                         if (!layer->numpasses) {
278                                 continue;
279                         }
280                         if (c + layer->len > dest + len) {
281                                 return -999;
282                         }
283                         
284                         memcpy(c, layer->data, layer->len);
285                         cblk->numpasses += layer->numpasses;
286                         c += layer->len;
287                         /* ADD for index Cfr. Marcela --> delta disto by packet */
288                         if(volume_info && volume_info->index_write && volume_info->index_on) {
289                                 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
290                                 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
291                                 info_PK->disto += layer->disto;
292                                 if (volume_info->D_max < info_PK->disto) {
293                                         volume_info->D_max = info_PK->disto;
294                                 }
295                         }
296                         /* </ADD> */
297                 }
298         }
299         
300         return (c - dest);
301 }
302
303 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
304         seg->numpasses = 0;
305         seg->len = 0;
306         if (cblksty & J3D_CCP_CBLKSTY_TERMALL) {
307                 seg->maxpasses = 1;
308         }
309         else if (cblksty & J3D_CCP_CBLKSTY_LAZY) {
310                 if (first) {
311                         seg->maxpasses = 10;
312                 } else {
313                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
314                 }
315         } else {
316                 seg->maxpasses = 109;
317         }
318 }
319
320 int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) {
321         int bandno, cblkno;
322         unsigned char *c = src;
323
324         opj_cp_t *cp = t2->cp;
325
326         int compno = pi->compno;        /* component value */
327         int resno  = pi->resno;         /* resolution level value */
328         int precno = pi->precno;        /* precinct value */
329         int layno  = pi->layno;         /* quality layer value */
330
331         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
332         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
333         
334         unsigned char *hd = NULL;
335         int present;
336         
337         opj_bio_t *bio = NULL;  /* BIO component */
338         
339         if (layno == 0) {
340                 for (bandno = 0; bandno < res->numbands; bandno++) {
341                         opj_tcd_band_t *band = &res->bands[bandno];
342                         opj_tcd_precinct_t *prc = &band->precincts[precno];
343                         
344                         if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
345
346                         tgt_reset(prc->incltree);
347                         tgt_reset(prc->imsbtree);
348                         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
349                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
350                                 cblk->numsegs = 0;
351                         }
352                 }
353         }
354         
355         /* SOP markers */
356         
357         if (tcp->csty & J3D_CP_CSTY_SOP) {
358                 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
359                         opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
360                 } else {
361                         c += 6;
362                 }
363                 
364                 /** TODO : check the Nsop value */
365         }
366         
367         /* 
368         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
369         This part deal with this caracteristic
370         step 1: Read packet header in the saved structure
371         step 2: Return to codestream for decoding 
372         */
373
374         bio = bio_create();
375         
376         if (cp->ppm == 1) {             /* PPM */
377                 hd = cp->ppm_data;
378                 bio_init_dec(bio, hd, cp->ppm_len);
379         } else if (tcp->ppt == 1) {     /* PPT */
380                 hd = tcp->ppt_data;
381                 bio_init_dec(bio, hd, tcp->ppt_len);
382         } else {                        /* Normal Case */
383                 hd = c;
384                 bio_init_dec(bio, hd, src+len-hd);
385         }
386         
387         present = bio_read(bio, 1);
388         
389         if (!present) {
390                 bio_inalign(bio);
391                 hd += bio_numbytes(bio);
392                 bio_destroy(bio);
393                 
394                 /* EPH markers */
395                 
396                 if (tcp->csty & J3D_CP_CSTY_EPH) {
397                         if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
398                                 printf("Error : expected EPH marker\n");
399                         } else {
400                                 hd += 2;
401                         }
402                 }
403                 
404                 if (cp->ppm == 1) {             /* PPM case */
405                         cp->ppm_len += cp->ppm_data-hd;
406                         cp->ppm_data = hd;
407                         return (c - src);
408                 }
409                 if (tcp->ppt == 1) {    /* PPT case */
410                         tcp->ppt_len+=tcp->ppt_data-hd;
411                         tcp->ppt_data = hd;
412                         return (c - src);
413                 }
414                 
415                 return (hd - src);
416         }
417         
418         for (bandno = 0; bandno < res->numbands; bandno++) {
419                 opj_tcd_band_t *band = &res->bands[bandno];
420                 opj_tcd_precinct_t *prc = &band->precincts[precno];
421                 
422                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
423                 
424                 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
425                         int included, increment, n;
426                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
427                         opj_tcd_seg_t *seg = NULL;
428                         /* if cblk not yet included before --> inclusion tagtree */
429                         if (!cblk->numsegs) {
430                 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
431                                 /* else one bit */
432                         } else {
433                                 included = bio_read(bio, 1);
434                         }
435                         /* if cblk not included */
436                         if (!included) {
437                                 cblk->numnewpasses = 0;
438                                 continue;
439                         }
440                         /* if cblk not yet included --> zero-bitplane tagtree */
441                         if (!cblk->numsegs) {
442                                 int i, numimsbs;
443                                 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++);
444                                 numimsbs = i - 1;
445                                 cblk->numbps = band->numbps - numimsbs;
446                                 cblk->numlenbits = 3;
447                         }
448                         /* number of coding passes */
449                         cblk->numnewpasses = t2_getnumpasses(bio);
450                         increment = t2_getcommacode(bio);
451                         /* length indicator increment */
452                         cblk->numlenbits += increment;
453                         if (!cblk->numsegs) {
454                                 seg = &cblk->segs[0];
455                                 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
456                         } else {
457                                 seg = &cblk->segs[cblk->numsegs - 1];
458                                 if (seg->numpasses == seg->maxpasses) {
459                                         t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
460                                 }
461                         }
462                         n = cblk->numnewpasses;
463                         
464                         do {
465                                 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
466                                 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
467                                 n -= seg->numnewpasses;
468                                 if (n > 0) {
469                                         t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
470                                 }
471                         } while (n > 0);
472                 }
473         }
474         
475         if (bio_inalign(bio)) {
476                 bio_destroy(bio);
477                 return -999;
478         }
479         
480         hd += bio_numbytes(bio);
481         bio_destroy(bio);
482         
483         /* EPH markers */
484         if (tcp->csty & J3D_CP_CSTY_EPH) {
485                 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
486                         opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
487                         return -999;
488                 } else {
489                         hd += 2;
490                 }
491         }
492         
493         if (cp->ppm==1) {
494                 cp->ppm_len+=cp->ppm_data-hd;
495                 cp->ppm_data = hd;
496         } else if (tcp->ppt == 1) {
497                 tcp->ppt_len+=tcp->ppt_data-hd;
498                 tcp->ppt_data = hd;
499         } else {
500                 c=hd;
501         }
502         
503         for (bandno = 0; bandno < res->numbands; bandno++) {
504                 opj_tcd_band_t *band = &res->bands[bandno];
505                 opj_tcd_precinct_t *prc = &band->precincts[precno];
506                 
507                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;             
508
509                 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
510                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
511                         opj_tcd_seg_t *seg = NULL;
512                         if (!cblk->numnewpasses)
513                                 continue;
514                         if (!cblk->numsegs) {
515                                 seg = &cblk->segs[0];
516                                 cblk->numsegs++;
517                                 cblk->len = 0;
518                         } else {
519                                 seg = &cblk->segs[cblk->numsegs - 1];
520                                 if (seg->numpasses == seg->maxpasses) {
521                                         seg++;
522                                         cblk->numsegs++;
523                                 }
524                         }
525                         
526                         do {
527                                 if (c + seg->newlen > src + len) {
528                                         return -999;
529                                 }
530                                 
531                                 memcpy(cblk->data + cblk->len, c, seg->newlen);
532                                 if (seg->numpasses == 0) {
533                                         seg->data = cblk->data + cblk->len;
534                                 }
535                                 c += seg->newlen;
536                                 cblk->len += seg->newlen;
537                                 seg->len += seg->newlen;
538                                 seg->numpasses += seg->numnewpasses;
539                                 cblk->numnewpasses -= seg->numnewpasses;
540                                 if (cblk->numnewpasses > 0) {
541                                         seg++;
542                                         cblk->numsegs++;
543                                 }
544                         } while (cblk->numnewpasses > 0);
545                 }
546         }
547         
548         return (c - src);
549 }
550
551 /* ----------------------------------------------------------------------- */
552
553 int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info) {
554         unsigned char *c = dest;
555         int e = 0;
556         opj_pi_iterator_t *pi = NULL;
557         int pino;
558
559         opj_volume_t *volume = t2->volume;
560         opj_cp_t *cp = t2->cp;
561         
562         /* create a packet iterator */
563         pi = pi_create(volume, cp, tileno);
564         if(!pi) {
565                 fprintf(stdout,"[ERROR] Failed to create a pi structure\n");
566                 return -999;
567         }
568         
569         if(volume_info) {
570                 volume_info->num = 0;
571         }
572         
573         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
574                 while (pi_next(&pi[pino])) {
575                         if (pi[pino].layno < maxlayers) {
576                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, volume_info, tileno, cp);
577                                 /*opj_event_msg(t2->cinfo, EVT_INFO, "  t2_encode_packet: %d bytes coded\n",e);*/
578                                 if (e == -999) {
579                                         break;
580                                 } else {
581                                         c += e;
582                                 }
583                                 
584                                 /* INDEX >> */
585                                 if(volume_info && volume_info->index_on) {
586                                         if(volume_info->index_write) {
587                                                 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
588                                                 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
589                                                 if (!volume_info->num) {
590                                                         info_PK->start_pos = info_TL->end_header + 1;
591                                                 } else {
592                                                         info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1;
593                                                 }
594                                                 info_PK->end_pos = info_PK->start_pos + e - 1;
595                                         }
596
597                                         volume_info->num++;
598                                 }
599                                 /* << INDEX */
600                         }
601                 }
602         }
603
604         /* don't forget to release pi */
605         pi_destroy(pi, cp, tileno);
606         
607         if (e == -999) {
608                 return e;
609         }
610
611     return (c - dest);
612 }
613
614 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
615         unsigned char *c = src;
616         opj_pi_iterator_t *pi;
617         int pino, e = 0;
618         int n = 0,i;
619
620         opj_volume_t *volume = t2->volume;
621         opj_cp_t *cp = t2->cp;
622         
623         /* create a packet iterator */
624         pi = pi_create(volume, cp, tileno);
625         if(!pi) {
626                 /* TODO: throw an error */
627                 return -999;
628         }
629         
630         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
631                 while (pi_next(&pi[pino])) {
632                         if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
633                                 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
634                         } else {
635                                 e = 0;
636                         }
637                         
638                         /* progression in resolution */
639                         for (i = 0; i < 3; i++){
640                 volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) : volume->comps[pi[pino].compno].resno_decoded[i];
641                         }
642                         n++;
643                         
644                         if (e == -999) {                /* ADD */
645                                 break;
646                         } else {
647                                 opj_event_msg(t2->cinfo, EVT_INFO, "  t2_decode_packet: %d bytes decoded\n",e);
648                                 c += e;
649                         }
650                 }
651         }
652
653         /* don't forget to release pi */
654         pi_destroy(pi, cp, tileno);
655         
656         if (e == -999) {
657                 return e;
658         }
659         
660     return (c - src);
661 }
662
663 /* ----------------------------------------------------------------------- */
664
665 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp) {
666         /* create the tcd structure */
667         opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
668         if(!t2) return NULL;
669         t2->cinfo = cinfo;
670         t2->volume = volume;
671         t2->cp = cp;
672
673         return t2;
674 }
675
676 void t2_destroy(opj_t2_t *t2) {
677         if(t2) {
678                 opj_free(t2);
679         }
680 }
681