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