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