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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
34 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
37 /** @name Local static functions */
40 static void t2_putcommacode(opj_bio_t *bio, int n);
41 static int t2_getcommacode(opj_bio_t *bio);
43 Variable length code for signalling delta Zil (truncation point)
44 @param bio Bit Input/Output component
47 static void t2_putnumpasses(opj_bio_t *bio, int n);
48 static int t2_getnumpasses(opj_bio_t *bio);
50 Encode a packet of a tile to a destination buffer
51 @param tile Tile for which to write the packets
52 @param tcp Tile coding parameters
53 @param pi Packet identity
54 @param dest Destination buffer
55 @param len Length of the destination buffer
56 @param cstr_info Codestream information structure
57 @param tileno Number of the tile encoded
60 static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_codestream_info_t *cstr_info, int tileno);
67 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first);
69 Decode a packet of a tile from a source buffer
71 @param src Source buffer
72 @param len Length of the source buffer
73 @param tile Tile for which to write the packets
74 @param tcp Tile coding parameters
75 @param pi Packet identity
76 @param pack_info Packet information
79 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
80 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info);
84 Decode a packet of a tile from a source buffer
86 @param src Source buffer
87 @param len Length of the source buffer
88 @param tile Tile for which to write the packets
89 @param tcp Tile coding parameters
90 @param pi Packet identity
93 static opj_bool t2_decode_packet_v2(
95 opj_tcd_tile_v2_t *p_tile,
97 opj_pi_iterator_t *p_pi,
99 OPJ_UINT32 * p_data_read,
100 OPJ_UINT32 p_max_length,
101 opj_packet_info_t *p_pack_info);
103 static opj_bool t2_skip_packet(
105 opj_tcd_tile_v2_t *p_tile,
107 opj_pi_iterator_t *p_pi,
109 OPJ_UINT32 * p_data_read,
110 OPJ_UINT32 p_max_length,
111 opj_packet_info_t *p_pack_info);
113 static opj_bool t2_read_packet_header(
115 opj_tcd_tile_v2_t *p_tile,
117 opj_pi_iterator_t *p_pi,
118 opj_bool * p_is_data_present,
119 OPJ_BYTE *p_src_data,
120 OPJ_UINT32 * p_data_read,
121 OPJ_UINT32 p_max_length,
122 opj_packet_info_t *p_pack_info);
124 static opj_bool t2_read_packet_data(
126 opj_tcd_tile_v2_t *p_tile,
127 opj_pi_iterator_t *p_pi,
128 OPJ_BYTE *p_src_data,
129 OPJ_UINT32 * p_data_read,
130 OPJ_UINT32 p_max_length,
131 opj_packet_info_t *pack_info);
133 static opj_bool t2_skip_packet_data(
135 opj_tcd_tile_v2_t *p_tile,
136 opj_pi_iterator_t *p_pi,
137 OPJ_UINT32 * p_data_read,
138 OPJ_UINT32 p_max_length,
139 opj_packet_info_t *pack_info);
146 static opj_bool t2_init_seg_v2( opj_tcd_cblk_dec_v2_t* cblk,
155 /* ----------------------------------------------------------------------- */
157 /* #define RESTART 0x04 */
159 static void t2_putcommacode(opj_bio_t *bio, int n) {
161 bio_write(bio, 1, 1);
163 bio_write(bio, 0, 1);
166 static int t2_getcommacode(opj_bio_t *bio) {
168 for (n = 0; bio_read(bio, 1); n++) {
174 static void t2_putnumpasses(opj_bio_t *bio, int n) {
176 bio_write(bio, 0, 1);
178 bio_write(bio, 2, 2);
180 bio_write(bio, 0xc | (n - 3), 4);
181 } else if (n <= 36) {
182 bio_write(bio, 0x1e0 | (n - 6), 9);
183 } else if (n <= 164) {
184 bio_write(bio, 0xff80 | (n - 37), 16);
188 static int t2_getnumpasses(opj_bio_t *bio) {
190 if (!bio_read(bio, 1))
192 if (!bio_read(bio, 1))
194 if ((n = bio_read(bio, 2)) != 3)
196 if ((n = bio_read(bio, 5)) != 31)
198 return (37 + bio_read(bio, 7));
201 static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_codestream_info_t *cstr_info, int tileno) {
203 unsigned char *c = dest;
205 int compno = pi->compno; /* component value */
206 int resno = pi->resno; /* resolution level value */
207 int precno = pi->precno; /* precinct value */
208 int layno = pi->layno; /* quality layer value */
210 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
211 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
213 opj_bio_t *bio = NULL; /* BIO component */
216 if (tcp->csty & J2K_CP_CSTY_SOP) {
221 c[4] = (unsigned char)((tile->packno % 65536) / 256);
222 c[5] = (unsigned char)((tile->packno % 65536) % 256);
228 for (bandno = 0; bandno < res->numbands; bandno++) {
229 opj_tcd_band_t *band = &res->bands[bandno];
230 opj_tcd_precinct_t *prc = &band->precincts[precno];
231 tgt_reset(prc->incltree);
232 tgt_reset(prc->imsbtree);
233 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
234 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
236 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
242 bio_init_enc(bio, c, length);
243 bio_write(bio, 1, 1); /* Empty header bit */
245 /* Writing Packet header */
246 for (bandno = 0; bandno < res->numbands; bandno++) {
247 opj_tcd_band_t *band = &res->bands[bandno];
248 opj_tcd_precinct_t *prc = &band->precincts[precno];
249 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
250 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
251 opj_tcd_layer_t *layer = &cblk->layers[layno];
252 if (!cblk->numpasses && layer->numpasses) {
253 tgt_setvalue(prc->incltree, cblkno, layno);
256 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
257 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
258 opj_tcd_layer_t *layer = &cblk->layers[layno];
262 /* cblk inclusion bits */
263 if (!cblk->numpasses) {
264 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
266 bio_write(bio, layer->numpasses != 0, 1);
268 /* if cblk not included, go to the next cblk */
269 if (!layer->numpasses) {
272 /* if first instance of cblk --> zero bit-planes information */
273 if (!cblk->numpasses) {
274 cblk->numlenbits = 3;
275 tgt_encode(bio, prc->imsbtree, cblkno, 999);
277 /* number of coding passes included */
278 t2_putnumpasses(bio, layer->numpasses);
280 /* computation of the increase of the length indicator and insertion in the header */
281 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
282 opj_tcd_pass_t *pass = &cblk->passes[passno];
285 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
286 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
291 t2_putcommacode(bio, increment);
293 /* computation of the new Length indicator */
294 cblk->numlenbits += increment;
296 /* insertion of the codeword segment length */
297 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
298 opj_tcd_pass_t *pass = &cblk->passes[passno];
301 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
302 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
310 if (bio_flush(bio)) {
312 return -999; /* modified to eliminate longjmp !! */
315 c += bio_numbytes(bio);
319 if (tcp->csty & J2K_CP_CSTY_EPH) {
327 // End of packet header position. Currently only represents the distance to start of packet
328 // Will be updated later by incrementing with packet start value
329 if(cstr_info && cstr_info->index_write) {
330 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
331 info_PK->end_ph_pos = (int)(c - dest);
335 /* Writing the packet body */
337 for (bandno = 0; bandno < res->numbands; bandno++) {
338 opj_tcd_band_t *band = &res->bands[bandno];
339 opj_tcd_precinct_t *prc = &band->precincts[precno];
340 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
341 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
342 opj_tcd_layer_t *layer = &cblk->layers[layno];
343 if (!layer->numpasses) {
346 if (c + layer->len > dest + length) {
350 memcpy(c, layer->data, layer->len);
351 cblk->numpasses += layer->numpasses;
354 if(cstr_info && cstr_info->index_write) {
355 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
356 info_PK->disto += layer->disto;
357 if (cstr_info->D_max < info_PK->disto) {
358 cstr_info->D_max = info_PK->disto;
368 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) {
370 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
371 seg = &cblk->segs[index];
376 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
379 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
383 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
386 seg->maxpasses = 109;
390 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
391 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
393 unsigned char *c = src;
395 opj_cp_t *cp = t2->cp;
397 int compno = pi->compno; /* component value */
398 int resno = pi->resno; /* resolution level value */
399 int precno = pi->precno; /* precinct value */
400 int layno = pi->layno; /* quality layer value */
402 opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno];
404 unsigned char *hd = NULL;
407 opj_bio_t *bio = NULL; /* BIO component */
410 for (bandno = 0; bandno < res->numbands; bandno++) {
411 opj_tcd_band_t *band = &res->bands[bandno];
412 opj_tcd_precinct_t *prc = &band->precincts[precno];
414 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
416 tgt_reset(prc->incltree);
417 tgt_reset(prc->imsbtree);
418 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
419 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
427 if (tcp->csty & J2K_CP_CSTY_SOP) {
428 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
429 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
434 /** TODO : check the Nsop value */
438 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
439 This part deal with this caracteristic
440 step 1: Read packet header in the saved structure
441 step 2: Return to codestream for decoding
446 if (cp->ppm == 1) { /* PPM */
448 bio_init_dec(bio, hd, cp->ppm_len);
449 } else if (tcp->ppt == 1) { /* PPT */
451 bio_init_dec(bio, hd, tcp->ppt_len);
452 } else { /* Normal Case */
454 bio_init_dec(bio, hd, src+len-hd);
457 present = bio_read(bio, 1);
461 hd += bio_numbytes(bio);
466 if (tcp->csty & J2K_CP_CSTY_EPH) {
467 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
468 printf("Error : expected EPH marker\n");
475 // End of packet header position. Currently only represents the distance to start of packet
476 // Will be updated later by incrementing with packet start value
478 pack_info->end_ph_pos = (int)(c - src);
482 if (cp->ppm == 1) { /* PPM case */
483 cp->ppm_len += cp->ppm_data-hd;
487 if (tcp->ppt == 1) { /* PPT case */
488 tcp->ppt_len+=tcp->ppt_data-hd;
496 for (bandno = 0; bandno < res->numbands; bandno++) {
497 opj_tcd_band_t *band = &res->bands[bandno];
498 opj_tcd_precinct_t *prc = &band->precincts[precno];
500 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
502 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
503 int included, increment, n, segno;
504 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
505 /* if cblk not yet included before --> inclusion tagtree */
506 if (!cblk->numsegs) {
507 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
510 included = bio_read(bio, 1);
512 /* if cblk not included */
514 cblk->numnewpasses = 0;
517 /* if cblk not yet included --> zero-bitplane tagtree */
518 if (!cblk->numsegs) {
520 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
524 cblk->numbps = band->numbps - numimsbs;
525 cblk->numlenbits = 3;
527 /* number of coding passes */
528 cblk->numnewpasses = t2_getnumpasses(bio);
529 increment = t2_getcommacode(bio);
530 /* length indicator increment */
531 cblk->numlenbits += increment;
533 if (!cblk->numsegs) {
534 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1);
536 segno = cblk->numsegs - 1;
537 if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
539 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
542 n = cblk->numnewpasses;
545 cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
546 cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
547 n -= cblk->segs[segno].numnewpasses;
550 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
556 if (bio_inalign(bio)) {
561 hd += bio_numbytes(bio);
565 if (tcp->csty & J2K_CP_CSTY_EPH) {
566 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
567 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
575 // End of packet header position. Currently only represents the distance to start of packet
576 // Will be updated later by incrementing with packet start value
578 pack_info->end_ph_pos = (int)(hd - src);
583 cp->ppm_len+=cp->ppm_data-hd;
585 } else if (tcp->ppt == 1) {
586 tcp->ppt_len+=tcp->ppt_data-hd;
592 for (bandno = 0; bandno < res->numbands; bandno++) {
593 opj_tcd_band_t *band = &res->bands[bandno];
594 opj_tcd_precinct_t *prc = &band->precincts[precno];
596 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
598 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
599 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
600 opj_tcd_seg_t *seg = NULL;
601 if (!cblk->numnewpasses)
603 if (!cblk->numsegs) {
604 seg = &cblk->segs[0];
608 seg = &cblk->segs[cblk->numsegs - 1];
609 if (seg->numpasses == seg->maxpasses) {
616 if (c + seg->newlen > src + len) {
621 /* we need here a j2k handle to verify if making a check to
622 the validity of cblocks parameters is selected from user (-W) */
624 /* let's check that we are not exceeding */
625 if ((cblk->len + seg->newlen) > 8192) {
626 opj_event_msg(t2->cinfo, EVT_WARNING,
627 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
628 seg->newlen, cblkno, precno, bandno, resno, compno);
630 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
633 seg->newlen = 8192 - cblk->len;
634 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
638 #endif /* USE_JPWL */
640 cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char*));
641 memcpy(cblk->data + cblk->len, c, seg->newlen);
642 if (seg->numpasses == 0) {
643 seg->data = &cblk->data;
644 seg->dataindex = cblk->len;
647 cblk->len += seg->newlen;
648 seg->len += seg->newlen;
649 seg->numpasses += seg->numnewpasses;
650 cblk->numnewpasses -= seg->numnewpasses;
651 if (cblk->numnewpasses > 0) {
655 } while (cblk->numnewpasses > 0);
662 /* ----------------------------------------------------------------------- */
664 int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode, int cur_totnum_tp){
665 unsigned char *c = dest;
668 opj_pi_iterator_t *pi = NULL;
670 opj_image_t *image = t2->image;
671 opj_cp_t *cp = t2->cp;
672 opj_tcp_t *tcp = &cp->tcps[tileno];
673 int pocno = cp->cinema == CINEMA4K_24? 2: 1;
674 int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
676 pi = pi_initialise_encode(image, cp, tileno, t2_mode);
678 /* TODO: throw an error */
682 if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
683 for(compno = 0; compno < maxcomp; compno++ ){
684 for(poc = 0; poc < pocno ; poc++){
687 if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
688 opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
689 pi_destroy(pi, cp, tileno);
692 while (pi_next(&pi[poc])) {
693 if (pi[poc].layno < maxlayers) {
694 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
695 comp_len = comp_len + e;
703 if (e == -999) break;
704 if (cp->max_comp_size){
705 if (comp_len > cp->max_comp_size){
711 if (e == -999) break;
713 }else{ /* t2_mode == FINAL_PASS */
714 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
715 while (pi_next(&pi[pino])) {
716 if (pi[pino].layno < maxlayers) {
717 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
725 if(cstr_info->index_write) {
726 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
727 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
728 if (!cstr_info->packno) {
729 info_PK->start_pos = info_TL->end_header + 1;
731 info_PK->start_pos = ((cp->tp_on | tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
733 info_PK->end_pos = info_PK->start_pos + e - 1;
734 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
735 // to start of packet is incremented by value of start of packet
746 pi_destroy(pi, cp, tileno);
755 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile, opj_codestream_info_t *cstr_info) {
756 unsigned char *c = src;
757 opj_pi_iterator_t *pi;
759 int n = 0, curtp = 0;
762 opj_image_t *image = t2->image;
763 opj_cp_t *cp = t2->cp;
765 /* create a packet iterator */
766 pi = pi_create_decode(image, cp, tileno);
768 /* TODO: throw an error */
774 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
775 while (pi_next(&pi[pino])) {
776 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
777 opj_packet_info_t *pack_info;
779 pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
782 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
786 if(e == -999) return -999;
787 /* progression in resolution */
788 image->comps[pi[pino].compno].resno_decoded =
790 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
791 : image->comps[pi[pino].compno].resno_decoded;
796 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
797 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
798 if (!cstr_info->packno) {
799 info_PK->start_pos = info_TL->end_header + 1;
800 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ // New tile part
801 info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
802 info_TL->tp[curtp].tp_start_pack = tp_start_packno;
803 tp_start_packno = cstr_info->packno;
805 info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
807 info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
809 info_PK->end_pos = info_PK->start_pos + e - 1;
810 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
811 // to start of packet is incremented by value of start of packet
816 if (e == -999) { /* ADD */
825 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in last tile-part
826 cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno;
830 /* don't forget to release pi */
831 pi_destroy(pi, cp, tileno);
840 opj_bool t2_decode_packets_v2(
842 OPJ_UINT32 p_tile_no,
843 struct opj_tcd_tile_v2 *p_tile,
845 OPJ_UINT32 * p_data_read,
846 OPJ_UINT32 p_max_len,
847 opj_codestream_index_t *p_cstr_index)
849 OPJ_BYTE *l_current_data = p_src;
850 opj_pi_iterator_t *l_pi = 00;
852 opj_image_t *l_image = p_t2->image;
853 opj_cp_v2_t *l_cp = p_t2->cp;
854 opj_cp_v2_t *cp = p_t2->cp;
855 opj_tcp_v2_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
856 OPJ_UINT32 l_nb_bytes_read;
857 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
858 opj_pi_iterator_t *l_current_pi = 00;
859 OPJ_UINT32 curtp = 0;
860 OPJ_UINT32 tp_start_packno;
861 opj_packet_info_t *l_pack_info = 00;
862 opj_image_comp_t* l_img_comp = 00;
866 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
870 /* create a packet iterator */
871 l_pi = pi_create_decode_v2(l_image, l_cp, p_tile_no);
879 for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
881 while (pi_next(l_current_pi)) {
883 if (l_tcp->num_layers_to_decode > l_current_pi->layno
884 && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
887 if (! t2_decode_packet_v2(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) {
888 pi_destroy_v2(l_pi,l_nb_pocs);
892 l_img_comp = &(l_image->comps[l_current_pi->compno]);
893 l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
897 if (! 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)) {
898 pi_destroy_v2(l_pi,l_nb_pocs);
903 l_current_data += l_nb_bytes_read;
904 p_max_len -= l_nb_bytes_read;
909 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
910 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
911 if (!p_cstr_info->packno) {
912 info_PK->start_pos = info_TL->end_header + 1;
913 } 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
914 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
915 tp_start_packno = p_cstr_info->packno;
917 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
919 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;
921 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
922 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
923 ++p_cstr_info->packno;
934 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
939 /* don't forget to release pi */
940 pi_destroy_v2(l_pi,l_nb_pocs);
941 *p_data_read = l_current_data - p_src;
945 /* ----------------------------------------------------------------------- */
947 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
948 /* create the tcd structure */
949 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
959 * Creates a Tier 2 handle
961 * @param p_image Source or destination image
962 * @param p_cp Image coding parameters.
963 * @return a new T2 handle if successful, NULL otherwise.
965 opj_t2_v2_t* t2_create_v2( opj_image_t *p_image,
968 /* create the tcd structure */
969 opj_t2_v2_t *l_t2 = (opj_t2_v2_t*)opj_malloc(sizeof(opj_t2_v2_t));
973 memset(l_t2,0,sizeof(opj_t2_t));
975 l_t2->image = p_image;
981 void t2_destroy(opj_t2_t *t2) {
987 void t2_destroy_v2(opj_t2_v2_t *t2) {
994 static opj_bool t2_decode_packet_v2(
996 opj_tcd_tile_v2_t *p_tile,
998 opj_pi_iterator_t *p_pi,
1000 OPJ_UINT32 * p_data_read,
1001 OPJ_UINT32 p_max_length,
1002 opj_packet_info_t *p_pack_info)
1004 opj_bool l_read_data;
1005 OPJ_UINT32 l_nb_bytes_read = 0;
1006 OPJ_UINT32 l_nb_total_bytes_read = 0;
1010 if (! 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)) {
1014 p_src += l_nb_bytes_read;
1015 l_nb_total_bytes_read += l_nb_bytes_read;
1016 p_max_length -= l_nb_bytes_read;
1018 /* we should read data for the packet */
1020 l_nb_bytes_read = 0;
1022 if (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
1026 l_nb_total_bytes_read += l_nb_bytes_read;
1029 *p_data_read = l_nb_total_bytes_read;
1034 static opj_bool t2_skip_packet(
1036 opj_tcd_tile_v2_t *p_tile,
1037 opj_tcp_v2_t *p_tcp,
1038 opj_pi_iterator_t *p_pi,
1040 OPJ_UINT32 * p_data_read,
1041 OPJ_UINT32 p_max_length,
1042 opj_packet_info_t *p_pack_info)
1044 opj_bool l_read_data;
1045 OPJ_UINT32 l_nb_bytes_read = 0;
1046 OPJ_UINT32 l_nb_total_bytes_read = 0;
1050 if (! 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)) {
1054 p_src += l_nb_bytes_read;
1055 l_nb_total_bytes_read += l_nb_bytes_read;
1056 p_max_length -= l_nb_bytes_read;
1058 /* we should read data for the packet */
1060 l_nb_bytes_read = 0;
1062 if (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
1066 l_nb_total_bytes_read += l_nb_bytes_read;
1068 *p_data_read = l_nb_total_bytes_read;
1075 static opj_bool t2_read_packet_header(
1077 opj_tcd_tile_v2_t *p_tile,
1078 opj_tcp_v2_t *p_tcp,
1079 opj_pi_iterator_t *p_pi,
1080 opj_bool * p_is_data_present,
1081 OPJ_BYTE *p_src_data,
1082 OPJ_UINT32 * p_data_read,
1083 OPJ_UINT32 p_max_length,
1084 opj_packet_info_t *p_pack_info)
1087 OPJ_UINT32 bandno, cblkno;
1088 OPJ_UINT32 l_nb_code_blocks;
1089 OPJ_UINT32 l_remaining_length;
1090 OPJ_UINT32 l_header_length;
1091 OPJ_UINT32 * l_modified_length_ptr = 00;
1092 OPJ_BYTE *l_current_data = p_src_data;
1093 opj_cp_v2_t *l_cp = p_t2->cp;
1094 opj_bio_t *l_bio = 00; /* BIO component */
1095 opj_tcd_band_v2_t *l_band = 00;
1096 opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1097 opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1099 OPJ_BYTE *l_header_data = 00;
1100 OPJ_BYTE **l_header_data_start = 00;
1102 OPJ_UINT32 l_present;
1104 if (p_pi->layno == 0) {
1105 l_band = l_res->bands;
1107 /* reset tagtrees */
1108 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1109 opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1111 if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
1112 tgt_reset(l_prc->incltree);
1113 tgt_reset(l_prc->imsbtree);
1114 l_cblk = l_prc->cblks.dec;
1116 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1117 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1118 l_cblk->numsegs = 0;
1119 l_cblk->real_num_segs = 0;
1130 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
1131 if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
1132 // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n");
1134 l_current_data += 6;
1137 /** TODO : check the Nsop value */
1141 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
1142 This part deal with this caracteristic
1143 step 1: Read packet header in the saved structure
1144 step 2: Return to codestream for decoding
1147 l_bio = bio_create();
1152 if (l_cp->ppm == 1) { /* PPM */
1153 l_header_data_start = &l_cp->ppm_data;
1154 l_header_data = *l_header_data_start;
1155 l_modified_length_ptr = &(l_cp->ppm_len);
1158 else if (p_tcp->ppt == 1) { /* PPT */
1159 l_header_data_start = &(p_tcp->ppt_data);
1160 l_header_data = *l_header_data_start;
1161 l_modified_length_ptr = &(p_tcp->ppt_len);
1163 else { /* Normal Case */
1164 l_header_data_start = &(l_current_data);
1165 l_header_data = *l_header_data_start;
1166 l_remaining_length = p_src_data+p_max_length-l_header_data;
1167 l_modified_length_ptr = &(l_remaining_length);
1170 bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
1172 l_present = bio_read(l_bio, 1);
1175 l_header_data += bio_numbytes(l_bio);
1179 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1180 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1181 printf("Error : expected EPH marker\n");
1187 l_header_length = (l_header_data - *l_header_data_start);
1188 *l_modified_length_ptr -= l_header_length;
1189 *l_header_data_start += l_header_length;
1192 // End of packet header position. Currently only represents the distance to start of packet
1193 // Will be updated later by incrementing with packet start value
1195 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1199 * p_is_data_present = OPJ_FALSE;
1200 *p_data_read = l_current_data - p_src_data;
1204 l_band = l_res->bands;
1205 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1206 opj_tcd_precinct_v2_t *l_prc = &(l_band->precincts[p_pi->precno]);
1208 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1213 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1214 l_cblk = l_prc->cblks.dec;
1215 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
1216 OPJ_UINT32 l_included,l_increment, l_segno;
1219 /* if cblk not yet included before --> inclusion tagtree */
1220 if (!l_cblk->numsegs) {
1221 l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1);
1225 l_included = bio_read(l_bio, 1);
1228 /* if cblk not included */
1230 l_cblk->numnewpasses = 0;
1235 /* if cblk not yet included --> zero-bitplane tagtree */
1236 if (!l_cblk->numsegs) {
1239 while (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i)) {
1243 l_cblk->numbps = l_band->numbps + 1 - i;
1244 l_cblk->numlenbits = 3;
1247 /* number of coding passes */
1248 l_cblk->numnewpasses = t2_getnumpasses(l_bio);
1249 l_increment = t2_getcommacode(l_bio);
1251 /* length indicator increment */
1252 l_cblk->numlenbits += l_increment;
1255 if (!l_cblk->numsegs) {
1256 if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1262 l_segno = l_cblk->numsegs - 1;
1263 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1265 if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1271 n = l_cblk->numnewpasses;
1274 l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n);
1275 l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1277 n -= l_cblk->segs[l_segno].numnewpasses;
1281 if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1294 if (bio_inalign(l_bio)) {
1299 l_header_data += bio_numbytes(l_bio);
1303 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1304 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1305 // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n");
1311 l_header_length = (l_header_data - *l_header_data_start);
1312 *l_modified_length_ptr -= l_header_length;
1313 *l_header_data_start += l_header_length;
1316 // End of packet header position. Currently only represents the distance to start of packet
1317 // Will be updated later by incrementing with packet start value
1319 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1323 *p_is_data_present = OPJ_TRUE;
1324 *p_data_read = l_current_data - p_src_data;
1329 static opj_bool t2_read_packet_data(
1331 opj_tcd_tile_v2_t *p_tile,
1332 opj_pi_iterator_t *p_pi,
1333 OPJ_BYTE *p_src_data,
1334 OPJ_UINT32 * p_data_read,
1335 OPJ_UINT32 p_max_length,
1336 opj_packet_info_t *pack_info)
1338 OPJ_UINT32 bandno, cblkno;
1339 OPJ_UINT32 l_nb_code_blocks;
1340 OPJ_BYTE *l_current_data = p_src_data;
1341 opj_tcd_band_v2_t *l_band = 00;
1342 opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1343 opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1345 l_band = l_res->bands;
1346 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1347 opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1349 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1354 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1355 l_cblk = l_prc->cblks.dec;
1357 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1358 opj_tcd_seg_t *l_seg = 00;
1360 if (!l_cblk->numnewpasses) {
1366 if (!l_cblk->numsegs) {
1367 l_seg = l_cblk->segs;
1372 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1374 if (l_seg->numpasses == l_seg->maxpasses) {
1381 if (l_current_data + l_seg->newlen > p_src_data + p_max_length) {
1386 /* we need here a j2k handle to verify if making a check to
1387 the validity of cblocks parameters is selected from user (-W) */
1389 /* let's check that we are not exceeding */
1390 if ((l_cblk->len + l_seg->newlen) > 8192) {
1391 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1392 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1393 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1395 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1398 l_seg->newlen = 8192 - l_cblk->len;
1399 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1403 #endif /* USE_JPWL */
1405 memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen);
1407 if (l_seg->numpasses == 0) {
1408 l_seg->data = &l_cblk->data;
1409 l_seg->dataindex = l_cblk->len;
1412 l_current_data += l_seg->newlen;
1413 l_seg->numpasses += l_seg->numnewpasses;
1414 l_cblk->numnewpasses -= l_seg->numnewpasses;
1416 l_seg->real_num_passes = l_seg->numpasses;
1417 l_cblk->len += l_seg->newlen;
1418 l_seg->len += l_seg->newlen;
1420 if (l_cblk->numnewpasses > 0) {
1424 } while (l_cblk->numnewpasses > 0);
1426 l_cblk->real_num_segs = l_cblk->numsegs;
1433 *(p_data_read) = l_current_data - p_src_data;
1438 static opj_bool t2_skip_packet_data(
1440 opj_tcd_tile_v2_t *p_tile,
1441 opj_pi_iterator_t *p_pi,
1442 OPJ_UINT32 * p_data_read,
1443 OPJ_UINT32 p_max_length,
1444 opj_packet_info_t *pack_info)
1446 OPJ_UINT32 bandno, cblkno;
1447 OPJ_UINT32 l_nb_code_blocks;
1448 opj_tcd_band_v2_t *l_band = 00;
1449 opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1450 opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1453 l_band = l_res->bands;
1455 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1456 opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1458 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1463 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1464 l_cblk = l_prc->cblks.dec;
1466 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1467 opj_tcd_seg_t *l_seg = 00;
1469 if (!l_cblk->numnewpasses) {
1475 if (!l_cblk->numsegs) {
1476 l_seg = l_cblk->segs;
1481 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1483 if (l_seg->numpasses == l_seg->maxpasses) {
1490 if (* p_data_read + l_seg->newlen > p_max_length) {
1495 /* we need here a j2k handle to verify if making a check to
1496 the validity of cblocks parameters is selected from user (-W) */
1498 /* let's check that we are not exceeding */
1499 if ((l_cblk->len + l_seg->newlen) > 8192) {
1500 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1501 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1502 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1504 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1507 l_seg->newlen = 8192 - l_cblk->len;
1508 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1512 #endif /* USE_JPWL */
1513 *(p_data_read) += l_seg->newlen;
1515 l_seg->numpasses += l_seg->numnewpasses;
1516 l_cblk->numnewpasses -= l_seg->numnewpasses;
1517 if (l_cblk->numnewpasses > 0)
1522 } while (l_cblk->numnewpasses > 0);
1534 static opj_bool t2_init_seg_v2(opj_tcd_cblk_dec_v2_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first)
1536 opj_tcd_seg_t* seg = 00;
1537 OPJ_UINT32 l_nb_segs = index + 1;
1539 if (l_nb_segs > cblk->m_current_max_segs) {
1540 cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS;
1542 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1548 seg = &cblk->segs[index];
1549 memset(seg,0,sizeof(opj_tcd_seg_t));
1551 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1554 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1556 seg->maxpasses = 10;
1558 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1561 seg->maxpasses = 109;