Merged changes in the trunk up to revision 28536.
[blender.git] / extern / 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                                         return -999;
618                                 }
619                                 while (pi_next(&pi[poc])) {
620                                         if (pi[poc].layno < maxlayers) {
621                                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
622                                                 comp_len = comp_len + e;
623                                                 if (e == -999) {
624                                                         break;
625                                                 } else {
626                                                         c += e;
627                                                 }
628                                         }
629                                 }
630                                 if (e == -999) break;
631                                 if (cp->max_comp_size){
632                                         if (comp_len > cp->max_comp_size){
633                                                 e = -999;
634                                                 break;
635                                         }
636                                 }
637                         }
638                         if (e == -999)  break;
639                 }
640         }else{  /* t2_mode == FINAL_PASS  */
641                 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
642                 while (pi_next(&pi[pino])) {
643                         if (pi[pino].layno < maxlayers) {
644                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
645                                 if (e == -999) {
646                                         break;
647                                 } else {
648                                         c += e;
649                                 }
650                                 /* INDEX >> */
651                                 if(cstr_info) {
652                                         if(cstr_info->index_write) {
653                                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
654                                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
655                                                 if (!cstr_info->packno) {
656                                                         info_PK->start_pos = info_TL->end_header + 1;
657                                                 } else {
658                                                         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;
659                                                 }
660                                                 info_PK->end_pos = info_PK->start_pos + e - 1;
661                                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance 
662                                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet
663                                         }
664                                         
665                                         cstr_info->packno++;
666                                 }
667                                 /* << INDEX */
668                                 tile->packno++;
669                         }
670                 }
671         }
672         
673         pi_destroy(pi, cp, tileno);
674         
675         if (e == -999) {
676                 return e;
677         }
678         
679   return (c - dest);
680 }
681
682 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) {
683         unsigned char *c = src;
684         opj_pi_iterator_t *pi;
685         int pino, e = 0;
686         int n = 0, curtp = 0;
687         int tp_start_packno;
688
689         opj_image_t *image = t2->image;
690         opj_cp_t *cp = t2->cp;
691         
692         /* create a packet iterator */
693         pi = pi_create_decode(image, cp, tileno);
694         if(!pi) {
695                 /* TODO: throw an error */
696                 return -999;
697         }
698
699         tp_start_packno = 0;
700         
701         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
702                 while (pi_next(&pi[pino])) {
703                         if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
704                                 opj_packet_info_t *pack_info;
705                                 if (cstr_info)
706                                         pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
707                                 else
708                                         pack_info = NULL;
709                                 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
710                         } else {
711                                 e = 0;
712                         }
713                         
714                         /* progression in resolution */
715                         image->comps[pi[pino].compno].resno_decoded =   
716                                 (e > 0) ? 
717                                 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
718                                 : image->comps[pi[pino].compno].resno_decoded;
719                         n++;
720
721                         /* INDEX >> */
722                         if(cstr_info) {
723                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
724                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
725                                 if (!cstr_info->packno) {
726                                         info_PK->start_pos = info_TL->end_header + 1;
727                                 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ // New tile part
728                                         info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
729                                         tp_start_packno = cstr_info->packno;
730                                         curtp++;
731                                         info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
732                                 } else {
733                                         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;
734                                 }
735                                 info_PK->end_pos = info_PK->start_pos + e - 1;
736                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  // End of packet header which now only represents the distance 
737                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet
738                                 cstr_info->packno++;
739                         }
740                         /* << INDEX */
741                         
742                         if (e == -999) {                /* ADD */
743                                 break;
744                         } else {
745                                 c += e;
746                         }                       
747                 }
748         }
749         /* INDEX >> */
750         if(cstr_info) {
751                 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in last tile-part
752         }
753         /* << INDEX */
754
755         /* don't forget to release pi */
756         pi_destroy(pi, cp, tileno);
757         
758         if (e == -999) {
759                 return e;
760         }
761         
762         return (c - src);
763 }
764
765 /* ----------------------------------------------------------------------- */
766
767 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
768         /* create the tcd structure */
769         opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
770         if(!t2) return NULL;
771         t2->cinfo = cinfo;
772         t2->image = image;
773         t2->cp = cp;
774
775         return t2;
776 }
777
778 void t2_destroy(opj_t2_t *t2) {
779         if(t2) {
780                 opj_free(t2);
781         }
782 }
783
784
785
786
787