Remove automatic setting of BUILDNAME for now
[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_cblk_dec_t* cblk, int index, 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_enc_t* cblk = &prc->cblks.enc[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_enc_t* cblk = &prc->cblks.enc[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_enc_t* cblk = &prc->cblks.enc[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_enc_t* cblk = &prc->cblks.enc[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_cblk_dec_t* cblk, int index, int cblksty, int first) {
298         opj_tcd_seg_t* seg;
299         cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
300         seg = &cblk->segs[index];
301         seg->data = NULL;
302         seg->dataindex = 0;
303         seg->numpasses = 0;
304         seg->len = 0;
305         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
306                 seg->maxpasses = 1;
307         }
308         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
309                 if (first) {
310                         seg->maxpasses = 10;
311                 } else {
312                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
313                 }
314         } else {
315                 seg->maxpasses = 109;
316         }
317 }
318
319 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, 
320                                                                                                                 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
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_resolution_t* res = &tile->comps[compno].resolutions[resno];
332
333         unsigned char *hd = NULL;
334         int present;
335         
336         opj_bio_t *bio = NULL;  /* BIO component */
337         
338         if (layno == 0) {
339                 for (bandno = 0; bandno < res->numbands; bandno++) {
340                         opj_tcd_band_t *band = &res->bands[bandno];
341                         opj_tcd_precinct_t *prc = &band->precincts[precno];
342                         
343                         if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
344                         
345                         tgt_reset(prc->incltree);
346                         tgt_reset(prc->imsbtree);
347                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
348                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
349                                 cblk->numsegs = 0;
350                         }
351                 }
352         }
353         
354         /* SOP markers */
355         
356         if (tcp->csty & J2K_CP_CSTY_SOP) {
357                 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
358                         opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
359                 } else {
360                         c += 6;
361                 }
362                 
363                 /** TODO : check the Nsop value */
364         }
365         
366         /* 
367         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
368         This part deal with this caracteristic
369         step 1: Read packet header in the saved structure
370         step 2: Return to codestream for decoding 
371         */
372
373         bio = bio_create();
374         
375         if (cp->ppm == 1) {             /* PPM */
376                 hd = cp->ppm_data;
377                 bio_init_dec(bio, hd, cp->ppm_len);
378         } else if (tcp->ppt == 1) {     /* PPT */
379                 hd = tcp->ppt_data;
380                 bio_init_dec(bio, hd, tcp->ppt_len);
381         } else {                        /* Normal Case */
382                 hd = c;
383                 bio_init_dec(bio, hd, src+len-hd);
384         }
385         
386         present = bio_read(bio, 1);
387         
388         if (!present) {
389                 bio_inalign(bio);
390                 hd += bio_numbytes(bio);
391                 bio_destroy(bio);
392                 
393                 /* EPH markers */
394                 
395                 if (tcp->csty & J2K_CP_CSTY_EPH) {
396                         if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
397                                 printf("Error : expected EPH marker\n");
398                         } else {
399                                 hd += 2;
400                         }
401                 }
402
403                 /* << INDEX */
404                 // End of packet header position. Currently only represents the distance to start of packet
405                 // Will be updated later by incrementing with packet start value
406                 if(pack_info) {
407                         pack_info->end_ph_pos = (int)(c - src);
408                 }
409                 /* INDEX >> */
410                 
411                 if (cp->ppm == 1) {             /* PPM case */
412                         cp->ppm_len += cp->ppm_data-hd;
413                         cp->ppm_data = hd;
414                         return (c - src);
415                 }
416                 if (tcp->ppt == 1) {    /* PPT case */
417                         tcp->ppt_len+=tcp->ppt_data-hd;
418                         tcp->ppt_data = hd;
419                         return (c - src);
420                 }
421                 
422                 return (hd - src);
423         }
424         
425         for (bandno = 0; bandno < res->numbands; bandno++) {
426                 opj_tcd_band_t *band = &res->bands[bandno];
427                 opj_tcd_precinct_t *prc = &band->precincts[precno];
428                 
429                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
430                 
431                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
432                         int included, increment, n, segno;
433                         opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
434                         /* if cblk not yet included before --> inclusion tagtree */
435                         if (!cblk->numsegs) {
436                                 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
437                                 /* else one bit */
438                         } else {
439                                 included = bio_read(bio, 1);
440                         }
441                         /* if cblk not included */
442                         if (!included) {
443                                 cblk->numnewpasses = 0;
444                                 continue;
445                         }
446                         /* if cblk not yet included --> zero-bitplane tagtree */
447                         if (!cblk->numsegs) {
448                                 int i, numimsbs;
449                                 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
450                                         ;
451                                 }
452                                 numimsbs = i - 1;
453                                 cblk->numbps = band->numbps - numimsbs;
454                                 cblk->numlenbits = 3;
455                         }
456                         /* number of coding passes */
457                         cblk->numnewpasses = t2_getnumpasses(bio);
458                         increment = t2_getcommacode(bio);
459                         /* length indicator increment */
460                         cblk->numlenbits += increment;
461                         segno = 0;
462                         if (!cblk->numsegs) {
463                                 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1);
464                         } else {
465                                 segno = cblk->numsegs - 1;
466                                 if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
467                                         ++segno;
468                                         t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
469                                 }
470                         }
471                         n = cblk->numnewpasses;
472                         
473                         do {
474                                 cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
475                                 cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
476                                 n -= cblk->segs[segno].numnewpasses;
477                                 if (n > 0) {
478                                         ++segno;
479                                         t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
480                                 }
481                         } while (n > 0);
482                 }
483         }
484         
485         if (bio_inalign(bio)) {
486                 bio_destroy(bio);
487                 return -999;
488         }
489         
490         hd += bio_numbytes(bio);
491         bio_destroy(bio);
492         
493         /* EPH markers */
494         if (tcp->csty & J2K_CP_CSTY_EPH) {
495                 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
496                         opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
497                 } else {
498                         hd += 2;
499                 }
500         }
501
502         /* << INDEX */
503         // End of packet header position. Currently only represents the distance to start of packet
504         // Will be updated later by incrementing with packet start value
505         if(pack_info) {
506                 pack_info->end_ph_pos = (int)(hd - src);
507         }
508         /* INDEX >> */
509         
510         if (cp->ppm==1) {
511                 cp->ppm_len+=cp->ppm_data-hd;
512                 cp->ppm_data = hd;
513         } else if (tcp->ppt == 1) {
514                 tcp->ppt_len+=tcp->ppt_data-hd;
515                 tcp->ppt_data = hd;
516         } else {
517                 c=hd;
518         }
519         
520         for (bandno = 0; bandno < res->numbands; bandno++) {
521                 opj_tcd_band_t *band = &res->bands[bandno];
522                 opj_tcd_precinct_t *prc = &band->precincts[precno];
523                 
524                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
525                 
526                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
527                         opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
528                         opj_tcd_seg_t *seg = NULL;
529                         if (!cblk->numnewpasses)
530                                 continue;
531                         if (!cblk->numsegs) {
532                                 seg = &cblk->segs[0];
533                                 cblk->numsegs++;
534                                 cblk->len = 0;
535                         } else {
536                                 seg = &cblk->segs[cblk->numsegs - 1];
537                                 if (seg->numpasses == seg->maxpasses) {
538                                         seg++;
539                                         cblk->numsegs++;
540                                 }
541                         }
542                         
543                         do {
544                                 if (c + seg->newlen > src + len) {
545                                         return -999;
546                                 }
547
548 #ifdef USE_JPWL
549                         /* we need here a j2k handle to verify if making a check to
550                         the validity of cblocks parameters is selected from user (-W) */
551
552                                 /* let's check that we are not exceeding */
553                                 if ((cblk->len + seg->newlen) > 8192) {
554                                         opj_event_msg(t2->cinfo, EVT_WARNING,
555                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
556                                                 seg->newlen, cblkno, precno, bandno, resno, compno);
557                                         if (!JPWL_ASSUME) {
558                                                 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
559                                                 return -999;
560                                         }
561                                         seg->newlen = 8192 - cblk->len;
562                                         opj_event_msg(t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", seg->newlen);
563                                         break;
564                                 };
565
566 #endif /* USE_JPWL */
567                                 
568                                 cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char*));
569                                 memcpy(cblk->data + cblk->len, c, seg->newlen);
570                                 if (seg->numpasses == 0) {
571                                         seg->data = &cblk->data;
572                                         seg->dataindex = cblk->len;
573                                 }
574                                 c += seg->newlen;
575                                 cblk->len += seg->newlen;
576                                 seg->len += seg->newlen;
577                                 seg->numpasses += seg->numnewpasses;
578                                 cblk->numnewpasses -= seg->numnewpasses;
579                                 if (cblk->numnewpasses > 0) {
580                                         seg++;
581                                         cblk->numsegs++;
582                                 }
583                         } while (cblk->numnewpasses > 0);
584                 }
585         }
586         
587         return (c - src);
588 }
589
590 /* ----------------------------------------------------------------------- */
591
592 int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode, int cur_totnum_tp){
593         unsigned char *c = dest;
594         int e = 0;
595         int compno;
596         opj_pi_iterator_t *pi = NULL;
597         int poc;
598         opj_image_t *image = t2->image;
599         opj_cp_t *cp = t2->cp;
600         opj_tcp_t *tcp = &cp->tcps[tileno];
601         int pocno = cp->cinema == CINEMA4K_24? 2: 1;
602         int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
603         
604         pi = pi_initialise_encode(image, cp, tileno, t2_mode);
605         if(!pi) {
606                 /* TODO: throw an error */
607                 return -999;
608         }
609         
610         if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
611                 for(compno = 0; compno < maxcomp; compno++ ){
612                         for(poc = 0; poc < pocno ; poc++){
613                                 int comp_len = 0;
614                                 int tpnum = compno;
615                                 if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
616                                         opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
617                                         pi_destroy(pi, cp, tileno);
618                                         return -999;
619                                 }
620                                 while (pi_next(&pi[poc])) {
621                                         if (pi[poc].layno < maxlayers) {
622                                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
623                                                 comp_len = comp_len + e;
624                                                 if (e == -999) {
625                                                         break;
626                                                 } else {
627                                                         c += e;
628                                                 }
629                                         }
630                                 }
631                                 if (e == -999) break;
632                                 if (cp->max_comp_size){
633                                         if (comp_len > cp->max_comp_size){
634                                                 e = -999;
635                                                 break;
636                                         }
637                                 }
638                         }
639                         if (e == -999)  break;
640                 }
641         }else{  /* t2_mode == FINAL_PASS  */
642                 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
643                 while (pi_next(&pi[pino])) {
644                         if (pi[pino].layno < maxlayers) {
645                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
646                                 if (e == -999) {
647                                         break;
648                                 } else {
649                                         c += e;
650                                 }
651                                 /* INDEX >> */
652                                 if(cstr_info) {
653                                         if(cstr_info->index_write) {
654                                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
655                                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
656                                                 if (!cstr_info->packno) {
657                                                         info_PK->start_pos = info_TL->end_header + 1;
658                                                 } else {
659                                                         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;
660                                                 }
661                                                 info_PK->end_pos = info_PK->start_pos + e - 1;
662                                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance 
663                                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet
664                                         }
665                                         
666                                         cstr_info->packno++;
667                                 }
668                                 /* << INDEX */
669                                 tile->packno++;
670                         }
671                 }
672         }
673         
674         pi_destroy(pi, cp, tileno);
675         
676         if (e == -999) {
677                 return e;
678         }
679         
680   return (c - dest);
681 }
682
683 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) {
684         unsigned char *c = src;
685         opj_pi_iterator_t *pi;
686         int pino, e = 0;
687         int n = 0, curtp = 0;
688         int tp_start_packno;
689
690         opj_image_t *image = t2->image;
691         opj_cp_t *cp = t2->cp;
692         
693         /* create a packet iterator */
694         pi = pi_create_decode(image, cp, tileno);
695         if(!pi) {
696                 /* TODO: throw an error */
697                 return -999;
698         }
699
700         tp_start_packno = 0;
701         
702         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
703                 while (pi_next(&pi[pino])) {
704                         if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
705                                 opj_packet_info_t *pack_info;
706                                 if (cstr_info)
707                                         pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
708                                 else
709                                         pack_info = NULL;
710                                 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
711                         } else {
712                                 e = 0;
713                         }
714                         
715                         /* progression in resolution */
716                         image->comps[pi[pino].compno].resno_decoded =   
717                                 (e > 0) ? 
718                                 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
719                                 : image->comps[pi[pino].compno].resno_decoded;
720                         n++;
721
722                         /* INDEX >> */
723                         if(cstr_info) {
724                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
725                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
726                                 if (!cstr_info->packno) {
727                                         info_PK->start_pos = info_TL->end_header + 1;
728                                 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ // New tile part
729                                         info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
730                                         tp_start_packno = cstr_info->packno;
731                                         curtp++;
732                                         info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
733                                 } else {
734                                         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;
735                                 }
736                                 info_PK->end_pos = info_PK->start_pos + e - 1;
737                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance 
738                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet
739                                 cstr_info->packno++;
740                         }
741                         /* << INDEX */
742                         
743                         if (e == -999) {                /* ADD */
744                                 break;
745                         } else {
746                                 c += e;
747                         }                       
748                 }
749         }
750         /* INDEX >> */
751         if(cstr_info) {
752                 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in last tile-part
753         }
754         /* << INDEX */
755
756         /* don't forget to release pi */
757         pi_destroy(pi, cp, tileno);
758         
759         if (e == -999) {
760                 return e;
761         }
762         
763         return (c - src);
764 }
765
766 /* ----------------------------------------------------------------------- */
767
768 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
769         /* create the tcd structure */
770         opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
771         if(!t2) return NULL;
772         t2->cinfo = cinfo;
773         t2->image = image;
774         t2->cp = cp;
775
776         return t2;
777 }
778
779 void t2_destroy(opj_t2_t *t2) {
780         if(t2) {
781                 opj_free(t2);
782         }
783 }
784
785
786
787
788