Reformat whole codebase with astyle.options (#128)
[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,
66                             opj_pi_iterator_t *pi, unsigned char *dest, int len,
67                             opj_volume_info_t *volume_info, int tileno, opj_cp_t *cp);
68 /**
69 Initialize the segment decoder
70 @param seg Segment instance
71 @param cblksty Codeblock style
72 @param first Is first segment
73 */
74 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
75 /**
76 Decode a packet of a tile from a source buffer
77 @param t2 T2 handle
78 @param src Source buffer
79 @param len Length of the source buffer
80 @param tile Tile for which to write the packets
81 @param tcp Tile coding parameters
82 @param pi Packet identity
83 @return Number of bytes decoded from the packet
84 */
85 int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len,
86                      opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi);
87
88 /*@}*/
89
90 /*@}*/
91
92 /* ----------------------------------------------------------------------- */
93
94 /* #define RESTART 0x04 */
95 static void t2_putcommacode(opj_bio_t *bio, int n)
96 {
97     while (--n >= 0) {
98         bio_write(bio, 1, 1);
99     }
100     bio_write(bio, 0, 1);
101 }
102
103 static int t2_getcommacode(opj_bio_t *bio)
104 {
105     int n;
106     for (n = 0; bio_read(bio, 1); n++) {
107         ;
108     }
109     return n;
110 }
111
112 static void t2_putnumpasses(opj_bio_t *bio, int n)
113 {
114     if (n == 1) {
115         bio_write(bio, 0, 1);
116     } else if (n == 2) {
117         bio_write(bio, 2, 2);
118     } else if (n <= 5) {
119         bio_write(bio, 0xc | (n - 3), 4);
120     } else if (n <= 36) {
121         bio_write(bio, 0x1e0 | (n - 6), 9);
122     } else if (n <= 164) {
123         bio_write(bio, 0xff80 | (n - 37), 16);
124     }
125 }
126
127 static int t2_getnumpasses(opj_bio_t *bio)
128 {
129     int n;
130     if (!bio_read(bio, 1)) {
131         return 1;
132     }
133     if (!bio_read(bio, 1)) {
134         return 2;
135     }
136     if ((n = bio_read(bio, 2)) != 3) {
137         return (3 + n);
138     }
139     if ((n = bio_read(bio, 5)) != 31) {
140         return (6 + n);
141     }
142     return (37 + bio_read(bio, 7));
143 }
144
145 static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp,
146                             opj_pi_iterator_t *pi, unsigned char *dest, int len,
147                             opj_volume_info_t * volume_info, int tileno, opj_cp_t *cp)
148 {
149     int bandno, cblkno;
150     unsigned char *sop = 0, *eph = 0;
151     unsigned char *c = dest;
152
153     int compno = pi->compno;    /* component value */
154     int resno  = pi->resno;     /* resolution level value */
155     int precno = pi->precno;    /* precinct value */
156     int layno  = pi->layno;     /* quality layer value */
157
158     opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
159     opj_tcd_resolution_t *res = &tilec->resolutions[resno];
160
161     opj_bio_t *bio = NULL;  /* BIO component */
162
163     /* <SOP 0xff91> */
164     if ((tcp->csty & J3D_CP_CSTY_SOP)) {
165         sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
166         sop[0] = 255;
167         sop[1] = 145;
168         sop[2] = 0;
169         sop[3] = 4;
170         sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ;
171         sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 65536) % 256 ;
172         memcpy(c, sop, 6);
173         opj_free(sop);
174         c += 6;
175     }
176     /* </SOP> */
177
178     if (!layno) {
179         for (bandno = 0; bandno < res->numbands; bandno++) {
180             opj_tcd_band_t *band = &res->bands[bandno];
181             opj_tcd_precinct_t *prc = &band->precincts[precno];
182             tgt_reset(prc->incltree);
183             tgt_reset(prc->imsbtree);
184             for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
185                     cblkno++) {
186                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
187                 cblk->numpasses = 0;
188                 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
189             }
190         }
191     }
192
193     bio = bio_create();
194     bio_init_enc(bio, c, len);
195     bio_write(bio, 1, 1);       /* Empty header bit */
196
197     /* Writing Packet header */
198     for (bandno = 0; bandno < res->numbands; bandno++) {
199         opj_tcd_band_t *band = &res->bands[bandno];
200         opj_tcd_precinct_t *prc = &band->precincts[precno];
201         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
202                 cblkno++) {
203             opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
204             opj_tcd_layer_t *layer = &cblk->layers[layno];
205             if (!cblk->numpasses && layer->numpasses) {
206                 tgt_setvalue(prc->incltree, cblkno, layno);
207             }
208         }
209
210         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
211                 cblkno++) {
212             opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
213             opj_tcd_layer_t *layer = &cblk->layers[layno];
214             int increment = 0;
215             int nump = 0;
216             int len = 0, passno;
217             /* cblk inclusion bits */
218             if (!cblk->numpasses) {
219                 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
220             } else {
221                 bio_write(bio, layer->numpasses != 0, 1);
222             }
223             /* if cblk not included, go to the next cblk  */
224             if (!layer->numpasses) {
225                 continue;
226             }
227             /* if first instance of cblk --> zero bit-planes information */
228             if (!cblk->numpasses) {
229                 cblk->numlenbits = 3;
230                 tgt_encode(bio, prc->imsbtree, cblkno, 999);
231             }
232             /* number of coding passes included */
233             t2_putnumpasses(bio, layer->numpasses);
234
235             /* computation of the increase of the length indicator and insertion in the header     */
236             for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses;
237                     passno++) {
238                 opj_tcd_pass_t *pass = &cblk->passes[passno];
239                 nump++;
240                 len += pass->len;
241                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
242                     increment = int_max(increment,
243                                         int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
244                     len = 0;
245                     nump = 0;
246                 }
247             }
248             t2_putcommacode(bio, increment);
249
250             /* computation of the new Length indicator */
251             cblk->numlenbits += increment;
252
253             /* insertion of the codeword segment length */
254             for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses;
255                     passno++) {
256                 opj_tcd_pass_t *pass = &cblk->passes[passno];
257                 nump++;
258                 len += pass->len;
259                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
260                     bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
261                     len = 0;
262                     nump = 0;
263                 }
264             }
265
266         }
267     }
268
269
270     if (bio_flush(bio)) {
271         return -999;        /* modified to eliminate longjmp !! */
272     }
273
274     c += bio_numbytes(bio);
275
276     bio_destroy(bio);
277
278     /* <EPH 0xff92> */
279     if (tcp->csty & J3D_CP_CSTY_EPH) {
280         eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
281         eph[0] = 255;
282         eph[1] = 146;
283         memcpy(c, eph, 2);
284         opj_free(eph);
285         c += 2;
286     }
287     /* </EPH> */
288
289     /* Writing the packet body */
290
291     for (bandno = 0; bandno < res->numbands; bandno++) {
292         opj_tcd_band_t *band = &res->bands[bandno];
293         opj_tcd_precinct_t *prc = &band->precincts[precno];
294         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
295                 cblkno++) {
296             opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
297             opj_tcd_layer_t *layer = &cblk->layers[layno];
298             if (!layer->numpasses) {
299                 continue;
300             }
301             if (c + layer->len > dest + len) {
302                 return -999;
303             }
304
305             memcpy(c, layer->data, layer->len);
306             cblk->numpasses += layer->numpasses;
307             c += layer->len;
308             /* ADD for index Cfr. Marcela --> delta disto by packet */
309             if (volume_info && volume_info->index_write && volume_info->index_on) {
310                 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
311                 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
312                 info_PK->disto += layer->disto;
313                 if (volume_info->D_max < info_PK->disto) {
314                     volume_info->D_max = info_PK->disto;
315                 }
316             }
317             /* </ADD> */
318         }
319     }
320
321     return (c - dest);
322 }
323
324 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first)
325 {
326     seg->numpasses = 0;
327     seg->len = 0;
328     if (cblksty & J3D_CCP_CBLKSTY_TERMALL) {
329         seg->maxpasses = 1;
330     } else if (cblksty & J3D_CCP_CBLKSTY_LAZY) {
331         if (first) {
332             seg->maxpasses = 10;
333         } else {
334             seg->maxpasses = (((seg - 1)->maxpasses == 1) ||
335                               ((seg - 1)->maxpasses == 10)) ? 2 : 1;
336         }
337     } else {
338         seg->maxpasses = 109;
339     }
340 }
341
342 int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len,
343                      opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi)
344 {
345     int bandno, cblkno;
346     unsigned char *c = src;
347
348     opj_cp_t *cp = t2->cp;
349
350     int compno = pi->compno;    /* component value */
351     int resno  = pi->resno;     /* resolution level value */
352     int precno = pi->precno;    /* precinct value */
353     int layno  = pi->layno;     /* quality layer value */
354
355     opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
356     opj_tcd_resolution_t *res = &tilec->resolutions[resno];
357
358     unsigned char *hd = NULL;
359     int present;
360
361     opj_bio_t *bio = NULL;  /* BIO component */
362
363     if (layno == 0) {
364         for (bandno = 0; bandno < res->numbands; bandno++) {
365             opj_tcd_band_t *band = &res->bands[bandno];
366             opj_tcd_precinct_t *prc = &band->precincts[precno];
367
368             if ((band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0) ||
369                     (band->z1 - band->z0 == 0)) {
370                 continue;
371             }
372
373             tgt_reset(prc->incltree);
374             tgt_reset(prc->imsbtree);
375             for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
376                     cblkno++) {
377                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
378                 cblk->numsegs = 0;
379             }
380         }
381     }
382
383     /* SOP markers */
384
385     if (tcp->csty & J3D_CP_CSTY_SOP) {
386         if ((*c) != 0xff || (*(c + 1) != 0x91)) {
387             opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
388         } else {
389             c += 6;
390         }
391
392         /** TODO : check the Nsop value */
393     }
394
395     /*
396     When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
397     This part deal with this caracteristic
398     step 1: Read packet header in the saved structure
399     step 2: Return to codestream for decoding
400     */
401
402     bio = bio_create();
403
404     if (cp->ppm == 1) {     /* PPM */
405         hd = cp->ppm_data;
406         bio_init_dec(bio, hd, cp->ppm_len);
407     } else if (tcp->ppt == 1) { /* PPT */
408         hd = tcp->ppt_data;
409         bio_init_dec(bio, hd, tcp->ppt_len);
410     } else {            /* Normal Case */
411         hd = c;
412         bio_init_dec(bio, hd, src + len - hd);
413     }
414
415     present = bio_read(bio, 1);
416
417     if (!present) {
418         bio_inalign(bio);
419         hd += bio_numbytes(bio);
420         bio_destroy(bio);
421
422         /* EPH markers */
423
424         if (tcp->csty & J3D_CP_CSTY_EPH) {
425             if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
426                 printf("Error : expected EPH marker\n");
427             } else {
428                 hd += 2;
429             }
430         }
431
432         if (cp->ppm == 1) {     /* PPM case */
433             cp->ppm_len += cp->ppm_data - hd;
434             cp->ppm_data = hd;
435             return (c - src);
436         }
437         if (tcp->ppt == 1) {    /* PPT case */
438             tcp->ppt_len += tcp->ppt_data - hd;
439             tcp->ppt_data = hd;
440             return (c - src);
441         }
442
443         return (hd - src);
444     }
445
446     for (bandno = 0; bandno < res->numbands; bandno++) {
447         opj_tcd_band_t *band = &res->bands[bandno];
448         opj_tcd_precinct_t *prc = &band->precincts[precno];
449
450         if ((band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0) ||
451                 (band->z1 - band->z0 == 0)) {
452             continue;
453         }
454
455         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
456                 cblkno++) {
457             int included, increment, n;
458             opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
459             opj_tcd_seg_t *seg = NULL;
460             /* if cblk not yet included before --> inclusion tagtree */
461             if (!cblk->numsegs) {
462                 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
463                 /* else one bit */
464             } else {
465                 included = bio_read(bio, 1);
466             }
467             /* if cblk not included */
468             if (!included) {
469                 cblk->numnewpasses = 0;
470                 continue;
471             }
472             /* if cblk not yet included --> zero-bitplane tagtree */
473             if (!cblk->numsegs) {
474                 int i, numimsbs;
475                 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++);
476                 numimsbs = i - 1;
477                 cblk->numbps = band->numbps - numimsbs;
478                 cblk->numlenbits = 3;
479             }
480             /* number of coding passes */
481             cblk->numnewpasses = t2_getnumpasses(bio);
482             increment = t2_getcommacode(bio);
483             /* length indicator increment */
484             cblk->numlenbits += increment;
485             if (!cblk->numsegs) {
486                 seg = &cblk->segs[0];
487                 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
488             } else {
489                 seg = &cblk->segs[cblk->numsegs - 1];
490                 if (seg->numpasses == seg->maxpasses) {
491                     t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
492                 }
493             }
494             n = cblk->numnewpasses;
495
496             do {
497                 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
498                 seg->newlen = bio_read(bio,
499                                        cblk->numlenbits + int_floorlog2(seg->numnewpasses));
500                 n -= seg->numnewpasses;
501                 if (n > 0) {
502                     t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
503                 }
504             } while (n > 0);
505         }
506     }
507
508     if (bio_inalign(bio)) {
509         bio_destroy(bio);
510         return -999;
511     }
512
513     hd += bio_numbytes(bio);
514     bio_destroy(bio);
515
516     /* EPH markers */
517     if (tcp->csty & J3D_CP_CSTY_EPH) {
518         if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
519             opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
520             return -999;
521         } else {
522             hd += 2;
523         }
524     }
525
526     if (cp->ppm == 1) {
527         cp->ppm_len += cp->ppm_data - hd;
528         cp->ppm_data = hd;
529     } else if (tcp->ppt == 1) {
530         tcp->ppt_len += tcp->ppt_data - hd;
531         tcp->ppt_data = hd;
532     } else {
533         c = hd;
534     }
535
536     for (bandno = 0; bandno < res->numbands; bandno++) {
537         opj_tcd_band_t *band = &res->bands[bandno];
538         opj_tcd_precinct_t *prc = &band->precincts[precno];
539
540         if ((band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0) ||
541                 (band->z1 - band->z0 == 0)) {
542             continue;
543         }
544
545         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
546                 cblkno++) {
547             opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
548             opj_tcd_seg_t *seg = NULL;
549             if (!cblk->numnewpasses) {
550                 continue;
551             }
552             if (!cblk->numsegs) {
553                 seg = &cblk->segs[0];
554                 cblk->numsegs++;
555                 cblk->len = 0;
556             } else {
557                 seg = &cblk->segs[cblk->numsegs - 1];
558                 if (seg->numpasses == seg->maxpasses) {
559                     seg++;
560                     cblk->numsegs++;
561                 }
562             }
563
564             do {
565                 if (c + seg->newlen > src + len) {
566                     return -999;
567                 }
568
569                 memcpy(cblk->data + cblk->len, c, seg->newlen);
570                 if (seg->numpasses == 0) {
571                     seg->data = cblk->data + cblk->len;
572                 }
573                 c += seg->newlen;
574                 cblk->len += seg->newlen;
575                 seg->len += seg->newlen;
576                 seg->numpasses += seg->numnewpasses;
577                 cblk->numnewpasses -= seg->numnewpasses;
578                 if (cblk->numnewpasses > 0) {
579                     seg++;
580                     cblk->numsegs++;
581                 }
582             } while (cblk->numnewpasses > 0);
583         }
584     }
585
586     return (c - src);
587 }
588
589 /* ----------------------------------------------------------------------- */
590
591 int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile,
592                       int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info)
593 {
594     unsigned char *c = dest;
595     int e = 0;
596     opj_pi_iterator_t *pi = NULL;
597     int pino;
598
599     opj_volume_t *volume = t2->volume;
600     opj_cp_t *cp = t2->cp;
601
602     /* create a packet iterator */
603     pi = pi_create(volume, cp, tileno);
604     if (!pi) {
605         fprintf(stdout, "[ERROR] Failed to create a pi structure\n");
606         return -999;
607     }
608
609     if (volume_info) {
610         volume_info->num = 0;
611     }
612
613     for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
614         while (pi_next(&pi[pino])) {
615             if (pi[pino].layno < maxlayers) {
616                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c,
617                                      volume_info, tileno, cp);
618                 /*opj_event_msg(t2->cinfo, EVT_INFO, "  t2_encode_packet: %d bytes coded\n",e);*/
619                 if (e == -999) {
620                     break;
621                 } else {
622                     c += e;
623                 }
624
625                 /* INDEX >> */
626                 if (volume_info && volume_info->index_on) {
627                     if (volume_info->index_write) {
628                         opj_tile_info_t *info_TL = &volume_info->tile[tileno];
629                         opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
630                         if (!volume_info->num) {
631                             info_PK->start_pos = info_TL->end_header + 1;
632                         } else {
633                             info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1;
634                         }
635                         info_PK->end_pos = info_PK->start_pos + e - 1;
636                     }
637
638                     volume_info->num++;
639                 }
640                 /* << INDEX */
641             }
642         }
643     }
644
645     /* don't forget to release pi */
646     pi_destroy(pi, cp, tileno);
647
648     if (e == -999) {
649         return e;
650     }
651
652     return (c - dest);
653 }
654
655 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno,
656                       opj_tcd_tile_t *tile)
657 {
658     unsigned char *c = src;
659     opj_pi_iterator_t *pi;
660     int pino, e = 0;
661     int n = 0, i;
662
663     opj_volume_t *volume = t2->volume;
664     opj_cp_t *cp = t2->cp;
665
666     /* create a packet iterator */
667     pi = pi_create(volume, cp, tileno);
668     if (!pi) {
669         /* TODO: throw an error */
670         return -999;
671     }
672
673     for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
674         while (pi_next(&pi[pino])) {
675             if ((cp->layer == 0) || (cp->layer >= ((pi[pino].layno) + 1))) {
676                 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
677             } else {
678                 e = 0;
679             }
680
681             /* progression in resolution */
682             for (i = 0; i < 3; i++) {
683                 volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(
684                             pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) :
685                         volume->comps[pi[pino].compno].resno_decoded[i];
686             }
687             n++;
688
689             if (e == -999) {        /* ADD */
690                 break;
691             } else {
692                 opj_event_msg(t2->cinfo, EVT_INFO, "  t2_decode_packet: %d bytes decoded\n", e);
693                 c += e;
694             }
695         }
696     }
697
698     /* don't forget to release pi */
699     pi_destroy(pi, cp, tileno);
700
701     if (e == -999) {
702         return e;
703     }
704
705     return (c - src);
706 }
707
708 /* ----------------------------------------------------------------------- */
709
710 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp)
711 {
712     /* create the tcd structure */
713     opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
714     if (!t2) {
715         return NULL;
716     }
717     t2->cinfo = cinfo;
718     t2->volume = volume;
719     t2->cp = cp;
720
721     return t2;
722 }
723
724 void t2_destroy(opj_t2_t *t2)
725 {
726     if (t2) {
727         opj_free(t2);
728     }
729 }
730