5a8d440c71678616a5886e01b321d5d083375855
[openjpeg.git] / src / lib / openjp2 / t2.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR 
15  * Copyright (c) 2012, CS Systemes d'Information, France
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 #include "opj_includes.h"
41
42 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
43 /*@{*/
44
45 /** @name Local static functions */
46 /*@{*/
47
48 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n);
49
50 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio); 
51 /**
52 Variable length code for signalling delta Zil (truncation point)
53 @param bio  Bit Input/Output component
54 @param n    delta Zil
55 */
56 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
57 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio);
58
59 /**
60 Encode a packet of a tile to a destination buffer
61 @param tileno Number of the tile encoded
62 @param tile Tile for which to write the packets
63 @param tcp Tile coding parameters
64 @param pi Packet identity
65 @param dest Destination buffer
66 @param p_data_written   FIXME DOC
67 @param len Length of the destination buffer
68 @param cstr_info Codestream information structure
69 @return
70 */
71 static OPJ_BOOL opj_t2_encode_packet(   OPJ_UINT32 tileno,
72                                         opj_tcd_tile_t *tile,
73                                         opj_tcp_t *tcp,
74                                         opj_pi_iterator_t *pi,
75                                         OPJ_BYTE *dest,
76                                         OPJ_UINT32 * p_data_written,
77                                         OPJ_UINT32 len,
78                                         opj_codestream_info_t *cstr_info);
79
80 /**
81 Decode a packet of a tile from a source buffer
82 @param t2 T2 handle
83 @param tile Tile for which to write the packets
84 @param tcp Tile coding parameters
85 @param pi Packet identity
86 @param src Source buffer
87 @param data_read   FIXME DOC
88 @param max_length  FIXME DOC
89 @param pack_info Packet information
90
91 @return  FIXME DOC
92 */
93 static OPJ_BOOL opj_t2_decode_packet(   opj_t2_t* t2,
94                                         opj_tcd_tile_t *tile,
95                                         opj_tcp_t *tcp,
96                                         opj_pi_iterator_t *pi,
97                                         OPJ_BYTE *src,
98                                         OPJ_UINT32 * data_read,
99                                         OPJ_UINT32 max_length,
100                                         opj_packet_info_t *pack_info,
101                                         opj_event_mgr_t *p_manager);
102
103 static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
104                                     opj_tcd_tile_t *p_tile,
105                                     opj_tcp_t *p_tcp,
106                                     opj_pi_iterator_t *p_pi,
107                                     OPJ_BYTE *p_src,
108                                     OPJ_UINT32 * p_data_read,
109                                     OPJ_UINT32 p_max_length,
110                                     opj_packet_info_t *p_pack_info,
111                                     opj_event_mgr_t *p_manager);
112
113 static OPJ_BOOL opj_t2_read_packet_header(  opj_t2_t* p_t2,
114                                             opj_tcd_tile_t *p_tile,
115                                             opj_tcp_t *p_tcp,
116                                             opj_pi_iterator_t *p_pi,
117                                             OPJ_BOOL * p_is_data_present,
118                                             OPJ_BYTE *p_src_data,
119                                             OPJ_UINT32 * p_data_read,
120                                             OPJ_UINT32 p_max_length,
121                                             opj_packet_info_t *p_pack_info,
122                                             opj_event_mgr_t *p_manager);
123
124 static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
125                                         opj_tcd_tile_t *p_tile,
126                                         opj_pi_iterator_t *p_pi,
127                                         OPJ_BYTE *p_src_data,
128                                         OPJ_UINT32 * p_data_read,
129                                         OPJ_UINT32 p_max_length,
130                                         opj_packet_info_t *pack_info,
131                                         opj_event_mgr_t *p_manager);
132
133 static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
134                                         opj_tcd_tile_t *p_tile,
135                                         opj_pi_iterator_t *p_pi,
136                                         OPJ_UINT32 * p_data_read,
137                                         OPJ_UINT32 p_max_length,
138                                         opj_packet_info_t *pack_info,
139                                         opj_event_mgr_t *p_manager);
140
141 /**
142 @param cblk
143 @param index
144 @param cblksty
145 @param first
146 */
147 static OPJ_BOOL opj_t2_init_seg(    opj_tcd_cblk_dec_t* cblk,
148                                     OPJ_UINT32 index,
149                                     OPJ_UINT32 cblksty,
150                                     OPJ_UINT32 first);
151
152 /*@}*/
153
154 /*@}*/
155
156 /* ----------------------------------------------------------------------- */
157
158 /* #define RESTART 0x04 */
159 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n) {
160         while (--n >= 0) {
161                 opj_bio_write(bio, 1, 1);
162         }
163         opj_bio_write(bio, 0, 1);
164 }
165
166 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
167 {
168     OPJ_UINT32 n = 0;
169     while (opj_bio_read(bio, 1)) {
170             ++n;
171     }
172     return n;
173 }
174
175 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) {
176         if (n == 1) {
177                 opj_bio_write(bio, 0, 1);
178         } else if (n == 2) {
179                 opj_bio_write(bio, 2, 2);
180         } else if (n <= 5) {
181                 opj_bio_write(bio, 0xc | (n - 3), 4);
182         } else if (n <= 36) {
183                 opj_bio_write(bio, 0x1e0 | (n - 6), 9);
184         } else if (n <= 164) {
185                 opj_bio_write(bio, 0xff80 | (n - 37), 16);
186         }
187 }
188
189 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio) {
190         OPJ_UINT32 n;
191         if (!opj_bio_read(bio, 1))
192                 return 1;
193         if (!opj_bio_read(bio, 1))
194                 return 2;
195         if ((n = opj_bio_read(bio, 2)) != 3)
196                 return (3 + n);
197         if ((n = opj_bio_read(bio, 5)) != 31)
198                 return (6 + n);
199         return (37 + opj_bio_read(bio, 7));
200 }
201
202 /* ----------------------------------------------------------------------- */
203
204 OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2,
205                                 OPJ_UINT32 p_tile_no,
206                                 opj_tcd_tile_t *p_tile,
207                                 OPJ_UINT32 p_maxlayers,
208                                 OPJ_BYTE *p_dest,
209                                 OPJ_UINT32 * p_data_written,
210                                 OPJ_UINT32 p_max_len,
211                                 opj_codestream_info_t *cstr_info,
212                                 OPJ_UINT32 p_tp_num,
213                                 OPJ_INT32 p_tp_pos,
214                                 OPJ_UINT32 p_pino,
215                                 J2K_T2_MODE p_t2_mode)
216 {
217         OPJ_BYTE *l_current_data = p_dest;
218         OPJ_UINT32 l_nb_bytes = 0;
219         OPJ_UINT32 compno;
220         OPJ_UINT32 poc;
221         opj_pi_iterator_t *l_pi = 00;
222         opj_pi_iterator_t *l_current_pi = 00;
223         opj_image_t *l_image = p_t2->image;
224         opj_cp_t *l_cp = p_t2->cp;
225         opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
226         OPJ_UINT32 pocno = (l_cp->rsiz == OPJ_PROFILE_CINEMA_4K)? 2: 1;
227         OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
228         OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
229
230         l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
231         if (!l_pi) {
232                 return OPJ_FALSE;
233         }
234
235         * p_data_written = 0;
236
237         if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */
238                 l_current_pi = l_pi;
239
240                 for     (compno = 0; compno < l_max_comp; ++compno) {
241                         OPJ_UINT32 l_comp_len = 0;
242                         l_current_pi = l_pi;
243
244                         for (poc = 0; poc < pocno ; ++poc) {
245                                 OPJ_UINT32 l_tp_num = compno;
246
247                                 /* TODO MSD : check why this function cannot fail (cf. v1) */
248                                 opj_pi_create_encode(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
249
250                                 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
251                                     /* TODO ADE : add an error */
252                                     opj_pi_destroy(l_pi, l_nb_pocs);
253                                     return OPJ_FALSE;
254                                 }
255                                 while (opj_pi_next(l_current_pi)) {
256                                         if (l_current_pi->layno < p_maxlayers) {
257                                                 l_nb_bytes = 0;
258
259                                                 if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
260                                                         opj_pi_destroy(l_pi, l_nb_pocs);
261                                                         return OPJ_FALSE;
262                                                 }
263
264                                                 l_comp_len += l_nb_bytes;
265                                                 l_current_data += l_nb_bytes;
266                                                 p_max_len -= l_nb_bytes;
267
268                                                 * p_data_written += l_nb_bytes;
269                                         }
270                                 }
271
272                                 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
273                                         if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
274                                                 opj_pi_destroy(l_pi, l_nb_pocs);
275                                                 return OPJ_FALSE;
276                                         }
277                                 }
278
279                                 ++l_current_pi;
280                         }
281                 }
282         }
283         else {  /* t2_mode == FINAL_PASS  */
284                 opj_pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
285
286                 l_current_pi = &l_pi[p_pino];
287                 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
288                     /* TODO ADE : add an error */
289                     opj_pi_destroy(l_pi, l_nb_pocs);
290                     return OPJ_FALSE;
291                 }
292                 while (opj_pi_next(l_current_pi)) {
293                         if (l_current_pi->layno < p_maxlayers) {
294                                 l_nb_bytes=0;
295
296                                 if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
297                                         opj_pi_destroy(l_pi, l_nb_pocs);
298                                         return OPJ_FALSE;
299                                 }
300
301                                 l_current_data += l_nb_bytes;
302                                 p_max_len -= l_nb_bytes;
303
304                                 * p_data_written += l_nb_bytes;
305
306                                 /* INDEX >> */
307                                 if(cstr_info) {
308                                         if(cstr_info->index_write) {
309                                                 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
310                                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
311                                                 if (!cstr_info->packno) {
312                                                         info_PK->start_pos = info_TL->end_header + 1;
313                                                 } else {
314                                                         info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
315                                                 }
316                                                 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
317                                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance
318                                                                                                                                                                                                                                                    to start of packet is incremented by value of start of packet*/
319                                         }
320
321                                         cstr_info->packno++;
322                                 }
323                                 /* << INDEX */
324                                 ++p_tile->packno;
325                         }
326                 }
327         }
328
329         opj_pi_destroy(l_pi, l_nb_pocs);
330
331         return OPJ_TRUE;
332 }
333
334 /* see issue 80 */
335 #if 0
336 #define JAS_FPRINTF fprintf
337 #else
338 /* issue 290 */
339 static void opj_null_jas_fprintf(FILE* file, const char * format, ...)
340 {
341   (void)file;
342   (void)format;
343 }
344 #define JAS_FPRINTF opj_null_jas_fprintf
345 #endif
346
347 OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2,
348                                 OPJ_UINT32 p_tile_no,
349                                 opj_tcd_tile_t *p_tile,
350                                 OPJ_BYTE *p_src,
351                                 OPJ_UINT32 * p_data_read,
352                                 OPJ_UINT32 p_max_len,
353                                 opj_codestream_index_t *p_cstr_index,
354                                 opj_event_mgr_t *p_manager)
355 {
356         OPJ_BYTE *l_current_data = p_src;
357         opj_pi_iterator_t *l_pi = 00;
358         OPJ_UINT32 pino;
359         opj_image_t *l_image = p_t2->image;
360         opj_cp_t *l_cp = p_t2->cp;
361         opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
362         OPJ_UINT32 l_nb_bytes_read;
363         OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
364         opj_pi_iterator_t *l_current_pi = 00;
365 #ifdef TODO_MSD
366         OPJ_UINT32 curtp = 0;
367         OPJ_UINT32 tp_start_packno;
368 #endif 
369         opj_packet_info_t *l_pack_info = 00;
370         opj_image_comp_t* l_img_comp = 00;
371
372         OPJ_ARG_NOT_USED(p_cstr_index);
373
374 #ifdef TODO_MSD
375         if (p_cstr_index) {
376                 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
377         }
378 #endif
379
380         /* create a packet iterator */
381         l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no);
382         if (!l_pi) {
383                 return OPJ_FALSE;
384         }
385
386
387         l_current_pi = l_pi;
388
389         for     (pino = 0; pino <= l_tcp->numpocs; ++pino) {
390
391                 /* if the resolution needed is too low, one dim of the tilec could be equal to zero
392                  * and no packets are used to decode this resolution and
393                  * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
394                  * and no l_img_comp->resno_decoded are computed
395                  */
396                 OPJ_BOOL* first_pass_failed = NULL;
397                                         
398                 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
399                     /* TODO ADE : add an error */
400                     opj_pi_destroy(l_pi, l_nb_pocs);
401                     return OPJ_FALSE;
402                 }
403                                         
404                 first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
405                 if (!first_pass_failed)
406                 {
407                     opj_pi_destroy(l_pi,l_nb_pocs);
408                     return OPJ_FALSE;
409                 }
410                 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
411
412                 while (opj_pi_next(l_current_pi)) {
413                   JAS_FPRINTF( stderr, "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
414                     l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno, l_current_pi->precno, l_current_pi->layno );
415
416                         if (l_tcp->num_layers_to_decode > l_current_pi->layno
417                                         && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
418                                 l_nb_bytes_read = 0;
419
420                                 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
421
422                                 if (! opj_t2_decode_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info, p_manager)) {
423                                         opj_pi_destroy(l_pi,l_nb_pocs);
424                                         opj_free(first_pass_failed);
425                                         return OPJ_FALSE;
426                                 }
427
428                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
429                                 l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
430                         }
431                         else {
432                                 l_nb_bytes_read = 0;
433                                 if (! opj_t2_skip_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info, p_manager)) {
434                                         opj_pi_destroy(l_pi,l_nb_pocs);
435                                         opj_free(first_pass_failed);
436                                         return OPJ_FALSE;
437                                 }
438                         }
439
440                         if (first_pass_failed[l_current_pi->compno]) {
441                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
442                                 if (l_img_comp->resno_decoded == 0)
443                                         l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
444                         }
445
446                         l_current_data += l_nb_bytes_read;
447                         p_max_len -= l_nb_bytes_read;
448
449                         /* INDEX >> */
450 #ifdef TODO_MSD
451                         if(p_cstr_info) {
452                                 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
453                                 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
454                                 tp_start_packno = 0;
455                                 if (!p_cstr_info->packno) {
456                                         info_PK->start_pos = info_TL->end_header + 1;
457                                 } else if (info_TL->packet[p_cstr_info->packno-1].end_pos >= (OPJ_INT32)p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos){ /* New tile part */
458                                         info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
459                                         tp_start_packno = p_cstr_info->packno;
460                                         curtp++;
461                                         info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
462                                 } else {
463                                         info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - 1].end_pos + 1;
464                                 }
465                                 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
466                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance */
467                                 ++p_cstr_info->packno;
468                         }
469 #endif
470                         /* << INDEX */
471                 }
472                 ++l_current_pi;
473
474                 opj_free(first_pass_failed);
475         }
476         /* INDEX >> */
477 #ifdef TODO_MSD
478         if
479                 (p_cstr_info) {
480                 p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part */
481         }
482 #endif
483         /* << INDEX */
484
485         /* don't forget to release pi */
486         opj_pi_destroy(l_pi,l_nb_pocs);
487         *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
488         return OPJ_TRUE;
489 }
490
491 /* ----------------------------------------------------------------------- */
492
493 /**
494  * Creates a Tier 2 handle
495  *
496  * @param       p_image         Source or destination image
497  * @param       p_cp            Image coding parameters.
498  * @return              a new T2 handle if successful, NULL otherwise.
499 */
500 opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
501 {
502         /* create the t2 structure */
503         opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1,sizeof(opj_t2_t));
504         if (!l_t2) {
505                 return NULL;
506         }
507
508         l_t2->image = p_image;
509         l_t2->cp = p_cp;
510
511         return l_t2;
512 }
513
514 void opj_t2_destroy(opj_t2_t *t2) {
515         if(t2) {
516                 opj_free(t2);
517         }
518 }
519
520 static OPJ_BOOL opj_t2_decode_packet(  opj_t2_t* p_t2,
521                                 opj_tcd_tile_t *p_tile,
522                                 opj_tcp_t *p_tcp,
523                                 opj_pi_iterator_t *p_pi,
524                                 OPJ_BYTE *p_src,
525                                 OPJ_UINT32 * p_data_read,
526                                 OPJ_UINT32 p_max_length,
527                                 opj_packet_info_t *p_pack_info,
528                                 opj_event_mgr_t *p_manager)
529 {
530         OPJ_BOOL l_read_data;
531         OPJ_UINT32 l_nb_bytes_read = 0;
532         OPJ_UINT32 l_nb_total_bytes_read = 0;
533
534         *p_data_read = 0;
535
536         if (! opj_t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info, p_manager)) {
537                 return OPJ_FALSE;
538         }
539
540         p_src += l_nb_bytes_read;
541         l_nb_total_bytes_read += l_nb_bytes_read;
542         p_max_length -= l_nb_bytes_read;
543
544         /* we should read data for the packet */
545         if (l_read_data) {
546                 l_nb_bytes_read = 0;
547
548                 if (! opj_t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info, p_manager)) {
549                         return OPJ_FALSE;
550                 }
551
552                 l_nb_total_bytes_read += l_nb_bytes_read;
553         }
554
555         *p_data_read = l_nb_total_bytes_read;
556
557         return OPJ_TRUE;
558 }
559
560 static OPJ_BOOL opj_t2_encode_packet(  OPJ_UINT32 tileno,
561                                 opj_tcd_tile_t * tile,
562                                 opj_tcp_t * tcp,
563                                 opj_pi_iterator_t *pi,
564                                 OPJ_BYTE *dest,
565                                 OPJ_UINT32 * p_data_written,
566                                 OPJ_UINT32 length,
567                                 opj_codestream_info_t *cstr_info)
568 {
569         OPJ_UINT32 bandno, cblkno;
570         OPJ_BYTE* c = dest;
571         OPJ_UINT32 l_nb_bytes;
572         OPJ_UINT32 compno = pi->compno;     /* component value */
573         OPJ_UINT32 resno  = pi->resno;      /* resolution level value */
574         OPJ_UINT32 precno = pi->precno;     /* precinct value */
575         OPJ_UINT32 layno  = pi->layno;      /* quality layer value */
576         OPJ_UINT32 l_nb_blocks;
577         opj_tcd_band_t *band = 00;
578         opj_tcd_cblk_enc_t* cblk = 00;
579         opj_tcd_pass_t *pass = 00;
580
581         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
582         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
583
584         opj_bio_t *bio = 00;    /* BIO component */
585
586         /* <SOP 0xff91> */
587         if (tcp->csty & J2K_CP_CSTY_SOP) {
588                 c[0] = 255;
589                 c[1] = 145;
590                 c[2] = 0;
591                 c[3] = 4;
592 #if 0
593                 c[4] = (tile->packno % 65536) / 256;
594                 c[5] = (tile->packno % 65536) % 256;
595 #else
596                 c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
597                 c[5] = tile->packno & 0xff;
598 #endif
599                 c += 6;
600                 length -= 6;
601         }
602         /* </SOP> */
603
604         if (!layno) {
605                 band = res->bands;
606
607                 for(bandno = 0; bandno < res->numbands; ++bandno) {
608                         opj_tcd_precinct_t *prc = &band->precincts[precno];
609
610                         opj_tgt_reset(prc->incltree);
611                         opj_tgt_reset(prc->imsbtree);
612
613                         l_nb_blocks = prc->cw * prc->ch;
614                         for     (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
615                                 cblk = &prc->cblks.enc[cblkno];
616
617                                 cblk->numpasses = 0;
618                                 opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
619                         }
620                         ++band;
621                 }
622         }
623
624         bio = opj_bio_create();
625         if (!bio) {
626                 /* FIXME event manager error callback */
627                 return OPJ_FALSE;
628         }
629         opj_bio_init_enc(bio, c, length);
630         opj_bio_write(bio, 1, 1);           /* Empty header bit */
631
632         /* Writing Packet header */
633         band = res->bands;
634         for (bandno = 0; bandno < res->numbands; ++bandno)      {
635                 opj_tcd_precinct_t *prc = &band->precincts[precno];
636
637                 l_nb_blocks = prc->cw * prc->ch;
638                 cblk = prc->cblks.enc;
639
640                 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
641                         opj_tcd_layer_t *layer = &cblk->layers[layno];
642
643                         if (!cblk->numpasses && layer->numpasses) {
644                                 opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
645                         }
646
647                         ++cblk;
648                 }
649
650                 cblk = prc->cblks.enc;
651                 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
652                         opj_tcd_layer_t *layer = &cblk->layers[layno];
653                         OPJ_UINT32 increment = 0;
654                         OPJ_UINT32 nump = 0;
655                         OPJ_UINT32 len = 0, passno;
656                         OPJ_UINT32 l_nb_passes;
657
658                         /* cblk inclusion bits */
659                         if (!cblk->numpasses) {
660                                 opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
661                         } else {
662                                 opj_bio_write(bio, layer->numpasses != 0, 1);
663                         }
664
665                         /* if cblk not included, go to the next cblk  */
666                         if (!layer->numpasses) {
667                                 ++cblk;
668                                 continue;
669                         }
670
671                         /* if first instance of cblk --> zero bit-planes information */
672                         if (!cblk->numpasses) {
673                                 cblk->numlenbits = 3;
674                                 opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
675                         }
676
677                         /* number of coding passes included */
678                         opj_t2_putnumpasses(bio, layer->numpasses);
679                         l_nb_passes = cblk->numpasses + layer->numpasses;
680                         pass = cblk->passes +  cblk->numpasses;
681
682                         /* computation of the increase of the length indicator and insertion in the header     */
683                         for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
684                                 ++nump;
685                                 len += pass->len;
686
687                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
688                                   increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment, opj_int_floorlog2((OPJ_INT32)len) + 1
689                                     - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
690                                         len = 0;
691                                         nump = 0;
692                                 }
693
694                                 ++pass;
695                         }
696                         opj_t2_putcommacode(bio, (OPJ_INT32)increment);
697
698                         /* computation of the new Length indicator */
699                         cblk->numlenbits += increment;
700
701                         pass = cblk->passes +  cblk->numpasses;
702                         /* insertion of the codeword segment length */
703                         for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
704                                 nump++;
705                                 len += pass->len;
706
707                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
708                                         opj_bio_write(bio, (OPJ_UINT32)len, cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
709                                         len = 0;
710                                         nump = 0;
711                                 }
712                                 ++pass;
713                         }
714
715                         ++cblk;
716                 }
717
718                 ++band;
719         }
720
721         if (!opj_bio_flush(bio)) {
722                 opj_bio_destroy(bio);
723                 return OPJ_FALSE;               /* modified to eliminate longjmp !! */
724         }
725
726         l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
727         c += l_nb_bytes;
728         length -= l_nb_bytes;
729
730         opj_bio_destroy(bio);
731
732         /* <EPH 0xff92> */
733         if (tcp->csty & J2K_CP_CSTY_EPH) {
734                 c[0] = 255;
735                 c[1] = 146;
736                 c += 2;
737                 length -= 2;
738         }
739         /* </EPH> */
740
741         /* << INDEX */
742         /* End of packet header position. Currently only represents the distance to start of packet
743            Will be updated later by incrementing with packet start value*/
744         if(cstr_info && cstr_info->index_write) {
745                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
746                 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
747         }
748         /* INDEX >> */
749
750         /* Writing the packet body */
751         band = res->bands;
752         for (bandno = 0; bandno < res->numbands; bandno++) {
753                 opj_tcd_precinct_t *prc = &band->precincts[precno];
754
755                 l_nb_blocks = prc->cw * prc->ch;
756                 cblk = prc->cblks.enc;
757
758                 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
759                         opj_tcd_layer_t *layer = &cblk->layers[layno];
760
761                         if (!layer->numpasses) {
762                                 ++cblk;
763                                 continue;
764                         }
765
766                         if (layer->len > length) {
767                                 return OPJ_FALSE;
768                         }
769
770                         memcpy(c, layer->data, layer->len);
771                         cblk->numpasses += layer->numpasses;
772                         c += layer->len;
773                         length -= layer->len;
774
775                         /* << INDEX */
776                         if(cstr_info && cstr_info->index_write) {
777                                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
778                                 info_PK->disto += layer->disto;
779                                 if (cstr_info->D_max < info_PK->disto) {
780                                         cstr_info->D_max = info_PK->disto;
781                                 }
782                         }
783
784                         ++cblk;
785                         /* INDEX >> */
786                 }
787                 ++band;
788         }
789
790         assert( c >= dest );
791         * p_data_written += (OPJ_UINT32)(c - dest);
792
793         return OPJ_TRUE;
794 }
795
796 static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
797                                     opj_tcd_tile_t *p_tile,
798                                     opj_tcp_t *p_tcp,
799                                     opj_pi_iterator_t *p_pi,
800                                     OPJ_BYTE *p_src,
801                                     OPJ_UINT32 * p_data_read,
802                                     OPJ_UINT32 p_max_length,
803                                     opj_packet_info_t *p_pack_info,
804                                     opj_event_mgr_t *p_manager)
805 {
806         OPJ_BOOL l_read_data;
807         OPJ_UINT32 l_nb_bytes_read = 0;
808         OPJ_UINT32 l_nb_total_bytes_read = 0;
809
810         *p_data_read = 0;
811
812         if (! opj_t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info, p_manager)) {
813                 return OPJ_FALSE;
814         }
815
816         p_src += l_nb_bytes_read;
817         l_nb_total_bytes_read += l_nb_bytes_read;
818         p_max_length -= l_nb_bytes_read;
819
820         /* we should read data for the packet */
821         if (l_read_data) {
822                 l_nb_bytes_read = 0;
823
824                 if (! opj_t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info, p_manager)) {
825                         return OPJ_FALSE;
826                 }
827
828                 l_nb_total_bytes_read += l_nb_bytes_read;
829         }
830         *p_data_read = l_nb_total_bytes_read;
831
832         return OPJ_TRUE;
833 }
834
835
836 static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2,
837                                     opj_tcd_tile_t *p_tile,
838                                     opj_tcp_t *p_tcp,
839                                     opj_pi_iterator_t *p_pi,
840                                     OPJ_BOOL * p_is_data_present,
841                                     OPJ_BYTE *p_src_data,
842                                     OPJ_UINT32 * p_data_read,
843                                     OPJ_UINT32 p_max_length,
844                                     opj_packet_info_t *p_pack_info,
845                                     opj_event_mgr_t *p_manager)
846
847 {
848         /* loop */
849         OPJ_UINT32 bandno, cblkno;
850         OPJ_UINT32 l_nb_code_blocks;
851         OPJ_UINT32 l_remaining_length;
852         OPJ_UINT32 l_header_length;
853         OPJ_UINT32 * l_modified_length_ptr = 00;
854         OPJ_BYTE *l_current_data = p_src_data;
855         opj_cp_t *l_cp = p_t2->cp;
856         opj_bio_t *l_bio = 00;  /* BIO component */
857         opj_tcd_band_t *l_band = 00;
858         opj_tcd_cblk_dec_t* l_cblk = 00;
859         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
860
861         OPJ_BYTE *l_header_data = 00;
862         OPJ_BYTE **l_header_data_start = 00;
863
864         OPJ_UINT32 l_present;
865
866         if (p_pi->layno == 0) {
867                 l_band = l_res->bands;
868
869                 /* reset tagtrees */
870                 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
871                         if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
872                                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
873                                 if (!(p_pi->precno < (l_band->precincts_data_size / sizeof(opj_tcd_precinct_t)))) {
874                                         opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct\n");
875                                         return OPJ_FALSE;
876                                 }
877                                                                                                         
878                                                                                                         
879                                 opj_tgt_reset(l_prc->incltree);
880                                 opj_tgt_reset(l_prc->imsbtree);
881                                 l_cblk = l_prc->cblks.dec;
882
883                                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
884                                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
885                                         l_cblk->numsegs = 0;
886                                         l_cblk->real_num_segs = 0;
887                                         ++l_cblk;
888                                 }
889                         }
890
891                         ++l_band;
892                 }
893         }
894
895         /* SOP markers */
896
897         if (p_tcp->csty & J2K_CP_CSTY_SOP) {
898                 if (p_max_length < 6) {
899                                                                                                 opj_event_msg(p_manager, EVT_WARNING, "Not enough space for expected SOP marker\n");
900                 } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
901                         opj_event_msg(p_manager, EVT_WARNING, "Expected SOP marker\n");
902                 } else {
903                         l_current_data += 6;
904                 }
905
906                 /** TODO : check the Nsop value */
907         }
908
909         /*
910         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
911         This part deal with this caracteristic
912         step 1: Read packet header in the saved structure
913         step 2: Return to codestream for decoding
914         */
915
916         l_bio = opj_bio_create();
917         if (! l_bio) {
918                 return OPJ_FALSE;
919         }
920
921         if (l_cp->ppm == 1) { /* PPM */
922                 l_header_data_start = &l_cp->ppm_data;
923                 l_header_data = *l_header_data_start;
924                 l_modified_length_ptr = &(l_cp->ppm_len);
925
926         }
927         else if (p_tcp->ppt == 1) { /* PPT */
928                 l_header_data_start = &(p_tcp->ppt_data);
929                 l_header_data = *l_header_data_start;
930                 l_modified_length_ptr = &(p_tcp->ppt_len);
931         }
932         else {  /* Normal Case */
933                 l_header_data_start = &(l_current_data);
934                 l_header_data = *l_header_data_start;
935                 l_remaining_length = (OPJ_UINT32)(p_src_data+p_max_length-l_header_data);
936                 l_modified_length_ptr = &(l_remaining_length);
937         }
938
939         opj_bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
940
941         l_present = opj_bio_read(l_bio, 1);
942         JAS_FPRINTF(stderr, "present=%d \n", l_present );
943         if (!l_present) {
944             /* TODO MSD: no test to control the output of this function*/
945                 opj_bio_inalign(l_bio);
946                 l_header_data += opj_bio_numbytes(l_bio);
947                 opj_bio_destroy(l_bio);
948
949                 /* EPH markers */
950                 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
951                         if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - *l_header_data_start)) < 2U) {
952                                 opj_event_msg(p_manager, EVT_WARNING, "Not enough space for expected EPH marker\n");
953                         } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
954                                 opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
955                         } else {
956                                 l_header_data += 2;
957                         }
958                 }
959
960                 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
961                 *l_modified_length_ptr -= l_header_length;
962                 *l_header_data_start += l_header_length;
963
964                 /* << INDEX */
965                 /* End of packet header position. Currently only represents the distance to start of packet
966                    Will be updated later by incrementing with packet start value */
967                 if (p_pack_info) {
968                         p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
969                 }
970                 /* INDEX >> */
971
972                 * p_is_data_present = OPJ_FALSE;
973                 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
974                 return OPJ_TRUE;
975         }
976
977         l_band = l_res->bands;
978         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
979                 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
980
981                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
982                         ++l_band;
983                         continue;
984                 }
985
986                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
987                 l_cblk = l_prc->cblks.dec;
988                 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
989                         OPJ_UINT32 l_included,l_increment, l_segno;
990                         OPJ_INT32 n;
991
992                         /* if cblk not yet included before --> inclusion tagtree */
993                         if (!l_cblk->numsegs) {
994                                 l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, (OPJ_INT32)(p_pi->layno + 1));
995                                 /* else one bit */
996                         }
997                         else {
998                                 l_included = opj_bio_read(l_bio, 1);
999                         }
1000
1001                         /* if cblk not included */
1002                         if (!l_included) {
1003                                 l_cblk->numnewpasses = 0;
1004                                 ++l_cblk;
1005                                 JAS_FPRINTF(stderr, "included=%d \n", l_included);
1006                                 continue;
1007                         }
1008
1009                         /* if cblk not yet included --> zero-bitplane tagtree */
1010                         if (!l_cblk->numsegs) {
1011                                 OPJ_UINT32 i = 0;
1012
1013                                 while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
1014                                         ++i;
1015                                 }
1016
1017                                 l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
1018                                 l_cblk->numlenbits = 3;
1019                         }
1020
1021                         /* number of coding passes */
1022                         l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
1023                         l_increment = opj_t2_getcommacode(l_bio);
1024
1025                         /* length indicator increment */
1026                         l_cblk->numlenbits += l_increment;
1027                         l_segno = 0;
1028
1029                         if (!l_cblk->numsegs) {
1030                                 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1031                                         opj_bio_destroy(l_bio);
1032                                         return OPJ_FALSE;
1033                                 }
1034                         }
1035                         else {
1036                                 l_segno = l_cblk->numsegs - 1;
1037                                 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1038                                         ++l_segno;
1039                                         if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1040                                                 opj_bio_destroy(l_bio);
1041                                                 return OPJ_FALSE;
1042                                         }
1043                                 }
1044                         }
1045                         n = (OPJ_INT32)l_cblk->numnewpasses;
1046
1047                         do {
1048                                 l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
1049                                 l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, l_cblk->numlenbits + opj_uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1050                                         JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n", l_included, l_cblk->segs[l_segno].numnewpasses, l_increment, l_cblk->segs[l_segno].newlen );
1051
1052                                 n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
1053                                 if (n > 0) {
1054                                         ++l_segno;
1055
1056                                         if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1057                                                 opj_bio_destroy(l_bio);
1058                                                 return OPJ_FALSE;
1059                                         }
1060                                 }
1061                         } while (n > 0);
1062
1063                         ++l_cblk;
1064                 }
1065
1066                 ++l_band;
1067         }
1068
1069         if (!opj_bio_inalign(l_bio)) {
1070                 opj_bio_destroy(l_bio);
1071                 return OPJ_FALSE;
1072         }
1073
1074         l_header_data += opj_bio_numbytes(l_bio);
1075         opj_bio_destroy(l_bio);
1076
1077         /* EPH markers */
1078         if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1079                 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - *l_header_data_start)) < 2U) {
1080                         opj_event_msg(p_manager, EVT_WARNING, "Not enough space for expected EPH marker\n");
1081                 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1082                         opj_event_msg(p_manager, EVT_WARNING, "Expected EPH marker\n");
1083                 } else {
1084                         l_header_data += 2;
1085                 }
1086         }
1087
1088         l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1089         JAS_FPRINTF( stderr, "hdrlen=%d \n", l_header_length );
1090         JAS_FPRINTF( stderr, "packet body\n");
1091         *l_modified_length_ptr -= l_header_length;
1092         *l_header_data_start += l_header_length;
1093
1094         /* << INDEX */
1095         /* End of packet header position. Currently only represents the distance to start of packet
1096          Will be updated later by incrementing with packet start value */
1097         if (p_pack_info) {
1098                 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1099         }
1100         /* INDEX >> */
1101
1102         *p_is_data_present = OPJ_TRUE;
1103         *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1104
1105         return OPJ_TRUE;
1106 }
1107
1108 static OPJ_BOOL opj_t2_read_packet_data(   opj_t2_t* p_t2,
1109                                     opj_tcd_tile_t *p_tile,
1110                                     opj_pi_iterator_t *p_pi,
1111                                     OPJ_BYTE *p_src_data,
1112                                     OPJ_UINT32 * p_data_read,
1113                                     OPJ_UINT32 p_max_length,
1114                                     opj_packet_info_t *pack_info,
1115                                     opj_event_mgr_t* p_manager)
1116 {
1117         OPJ_UINT32 bandno, cblkno;
1118         OPJ_UINT32 l_nb_code_blocks;
1119         OPJ_BYTE *l_current_data = p_src_data;
1120         opj_tcd_band_t *l_band = 00;
1121         opj_tcd_cblk_dec_t* l_cblk = 00;
1122         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1123
1124         OPJ_ARG_NOT_USED(p_t2);
1125         OPJ_ARG_NOT_USED(pack_info);
1126
1127         l_band = l_res->bands;
1128         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1129                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1130
1131                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1132                         ++l_band;
1133                         continue;
1134                 }
1135
1136                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1137                 l_cblk = l_prc->cblks.dec;
1138
1139                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1140                         opj_tcd_seg_t *l_seg = 00;
1141
1142                         if (!l_cblk->numnewpasses) {
1143                                 /* nothing to do */
1144                                 ++l_cblk;
1145                                 continue;
1146                         }
1147
1148                         if (!l_cblk->numsegs) {
1149                                 l_seg = l_cblk->segs;
1150                                 ++l_cblk->numsegs;
1151                                 l_cblk->data_current_size = 0;
1152                         }
1153                         else {
1154                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1155
1156                                 if (l_seg->numpasses == l_seg->maxpasses) {
1157                                         ++l_seg;
1158                                         ++l_cblk->numsegs;
1159                                 }
1160                         }
1161
1162                         do {
1163                                 /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
1164                                 if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) < (OPJ_SIZE_T)l_current_data) || (l_current_data + l_seg->newlen > p_src_data + p_max_length)) {
1165                                         opj_event_msg(p_manager, EVT_ERROR, "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1166                                                                                                                                                                                                 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1167                                         return OPJ_FALSE;
1168                                 }
1169
1170 #ifdef USE_JPWL
1171                         /* we need here a j2k handle to verify if making a check to
1172                         the validity of cblocks parameters is selected from user (-W) */
1173
1174                                 /* let's check that we are not exceeding */
1175                                 if ((l_cblk->len + l_seg->newlen) > 8192) {
1176                                         opj_event_msg(p_manager, EVT_WARNING,
1177                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1178                                                 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1179                                         if (!JPWL_ASSUME) {
1180                                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
1181                                                 return OPJ_FALSE;
1182                                         }
1183                                         l_seg->newlen = 8192 - l_cblk->len;
1184                                         opj_event_msg(p_manager, EVT_WARNING, "      - truncating segment to %d\n", l_seg->newlen);
1185                                         break;
1186                                 };
1187
1188 #endif /* USE_JPWL */
1189                                 /* Check possible overflow on size */
1190                                 if ((l_cblk->data_current_size + l_seg->newlen) < l_cblk->data_current_size) {
1191                                         opj_event_msg(p_manager, EVT_ERROR, "read: segment too long (%d) with current size (%d > %d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1192                                                 l_seg->newlen, l_cblk->data_current_size, 0xFFFFFFFF - l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1193                                         return OPJ_FALSE;
1194                                 }
1195                                 /* Check if the cblk->data have allocated enough memory */
1196                                 if ((l_cblk->data_current_size + l_seg->newlen) > l_cblk->data_max_size) {
1197                                     OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_realloc(l_cblk->data, l_cblk->data_current_size + l_seg->newlen);
1198                                     if(! new_cblk_data) {
1199                                         opj_free(l_cblk->data);
1200                                         l_cblk->data = NULL;
1201                                         l_cblk->data_max_size = 0;
1202                                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to realloc code block cata!\n"); */
1203                                         return OPJ_FALSE;
1204                                     }
1205                                     l_cblk->data_max_size = l_cblk->data_current_size + l_seg->newlen;
1206                                     l_cblk->data = new_cblk_data;
1207                                 }
1208                                
1209                                 memcpy(l_cblk->data + l_cblk->data_current_size, l_current_data, l_seg->newlen);
1210
1211                                 if (l_seg->numpasses == 0) {
1212                                         l_seg->data = &l_cblk->data;
1213                                         l_seg->dataindex = l_cblk->data_current_size;
1214                                 }
1215
1216                                 l_current_data += l_seg->newlen;
1217                                 l_seg->numpasses += l_seg->numnewpasses;
1218                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1219
1220                                 l_seg->real_num_passes = l_seg->numpasses;
1221                                 l_cblk->data_current_size += l_seg->newlen;
1222                                 l_seg->len += l_seg->newlen;
1223
1224                                 if (l_cblk->numnewpasses > 0) {
1225                                         ++l_seg;
1226                                         ++l_cblk->numsegs;
1227                                 }
1228                         } while (l_cblk->numnewpasses > 0);
1229
1230                         l_cblk->real_num_segs = l_cblk->numsegs;
1231                         ++l_cblk;
1232                 } /* next code_block */
1233
1234                 ++l_band;
1235         }
1236
1237         *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
1238
1239
1240         return OPJ_TRUE;
1241 }
1242
1243 static OPJ_BOOL opj_t2_skip_packet_data(   opj_t2_t* p_t2,
1244                                     opj_tcd_tile_t *p_tile,
1245                                     opj_pi_iterator_t *p_pi,
1246                                     OPJ_UINT32 * p_data_read,
1247                                     OPJ_UINT32 p_max_length,
1248                                     opj_packet_info_t *pack_info,
1249                                     opj_event_mgr_t *p_manager)
1250 {
1251         OPJ_UINT32 bandno, cblkno;
1252         OPJ_UINT32 l_nb_code_blocks;
1253         opj_tcd_band_t *l_band = 00;
1254         opj_tcd_cblk_dec_t* l_cblk = 00;
1255         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1256
1257         OPJ_ARG_NOT_USED(p_t2);
1258         OPJ_ARG_NOT_USED(pack_info);
1259
1260         *p_data_read = 0;
1261         l_band = l_res->bands;
1262
1263         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1264                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1265
1266                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1267                         ++l_band;
1268                         continue;
1269                 }
1270
1271                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1272                 l_cblk = l_prc->cblks.dec;
1273
1274                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1275                         opj_tcd_seg_t *l_seg = 00;
1276
1277                         if (!l_cblk->numnewpasses) {
1278                                 /* nothing to do */
1279                                 ++l_cblk;
1280                                 continue;
1281                         }
1282
1283                         if (!l_cblk->numsegs) {
1284                                 l_seg = l_cblk->segs;
1285                                 ++l_cblk->numsegs;
1286                                 l_cblk->data_current_size = 0;
1287                         }
1288                         else {
1289                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1290
1291                                 if (l_seg->numpasses == l_seg->maxpasses) {
1292                                         ++l_seg;
1293                                         ++l_cblk->numsegs;
1294                                 }
1295                         }
1296
1297                         do {
1298                                 /* Check possible overflow then size */
1299                                 if (((*p_data_read + l_seg->newlen) < (*p_data_read)) || ((*p_data_read + l_seg->newlen) > p_max_length)) {
1300                                         opj_event_msg(p_manager, EVT_ERROR, "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1301                                                 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1302                                         return OPJ_FALSE;
1303                                 }
1304
1305 #ifdef USE_JPWL
1306                         /* we need here a j2k handle to verify if making a check to
1307                         the validity of cblocks parameters is selected from user (-W) */
1308
1309                                 /* let's check that we are not exceeding */
1310                                 if ((l_cblk->len + l_seg->newlen) > 8192) {
1311                                         opj_event_msg(p_manager, EVT_WARNING,
1312                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1313                                                 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1314                                         if (!JPWL_ASSUME) {
1315                                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
1316                                                 return -999;
1317                                         }
1318                                         l_seg->newlen = 8192 - l_cblk->len;
1319                                         opj_event_msg(p_manager, EVT_WARNING, "      - truncating segment to %d\n", l_seg->newlen);
1320                                         break;
1321                                 };
1322
1323 #endif /* USE_JPWL */
1324                                         JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read, l_seg->newlen );
1325                                 *(p_data_read) += l_seg->newlen;
1326
1327                                 l_seg->numpasses += l_seg->numnewpasses;
1328                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1329                                 if (l_cblk->numnewpasses > 0)
1330                                 {
1331                                         ++l_seg;
1332                                         ++l_cblk->numsegs;
1333                                 }
1334                         } while (l_cblk->numnewpasses > 0);
1335
1336                         ++l_cblk;
1337                 }
1338
1339                 ++l_band;
1340         }
1341
1342         return OPJ_TRUE;
1343 }
1344
1345
1346 static OPJ_BOOL opj_t2_init_seg(   opj_tcd_cblk_dec_t* cblk,
1347                             OPJ_UINT32 index, 
1348                             OPJ_UINT32 cblksty, 
1349                             OPJ_UINT32 first)
1350 {
1351         opj_tcd_seg_t* seg = 00;
1352         OPJ_UINT32 l_nb_segs = index + 1;
1353
1354         if (l_nb_segs > cblk->m_current_max_segs) {
1355                 opj_tcd_seg_t* new_segs;
1356                 cblk->m_current_max_segs += OPJ_J2K_DEFAULT_NB_SEGS;
1357
1358                 new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1359                 if(! new_segs) {
1360                         opj_free(cblk->segs);
1361                         cblk->segs = NULL;
1362                         cblk->m_current_max_segs = 0;
1363                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
1364                         return OPJ_FALSE;
1365                 }
1366                 cblk->segs = new_segs;
1367         }
1368
1369         seg = &cblk->segs[index];
1370         memset(seg,0,sizeof(opj_tcd_seg_t));
1371
1372         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1373                 seg->maxpasses = 1;
1374         }
1375         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1376                 if (first) {
1377                         seg->maxpasses = 10;
1378                 } else {
1379                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1380                 }
1381         } else {
1382                 seg->maxpasses = 109;
1383         }
1384
1385         return OPJ_TRUE;
1386 }