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