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_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_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_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_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_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_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first);
152 /* ----------------------------------------------------------------------- */
154 /* #define RESTART 0x04 */
156 static void t2_putcommacode(opj_bio_t *bio, int n) {
158 bio_write(bio, 1, 1);
160 bio_write(bio, 0, 1);
163 static int t2_getcommacode(opj_bio_t *bio) {
165 for (n = 0; bio_read(bio, 1); n++) {
171 static void t2_putnumpasses(opj_bio_t *bio, int n) {
173 bio_write(bio, 0, 1);
175 bio_write(bio, 2, 2);
177 bio_write(bio, 0xc | (n - 3), 4);
178 } else if (n <= 36) {
179 bio_write(bio, 0x1e0 | (n - 6), 9);
180 } else if (n <= 164) {
181 bio_write(bio, 0xff80 | (n - 37), 16);
185 static int t2_getnumpasses(opj_bio_t *bio) {
187 if (!bio_read(bio, 1))
189 if (!bio_read(bio, 1))
191 if ((n = bio_read(bio, 2)) != 3)
193 if ((n = bio_read(bio, 5)) != 31)
195 return (37 + bio_read(bio, 7));
198 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) {
200 unsigned char *c = dest;
202 int compno = pi->compno; /* component value */
203 int resno = pi->resno; /* resolution level value */
204 int precno = pi->precno; /* precinct value */
205 int layno = pi->layno; /* quality layer value */
207 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
208 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
210 opj_bio_t *bio = NULL; /* BIO component */
213 if (tcp->csty & J2K_CP_CSTY_SOP) {
218 c[4] = (unsigned char)((tile->packno % 65536) / 256);
219 c[5] = (unsigned char)((tile->packno % 65536) % 256);
225 for (bandno = 0; bandno < res->numbands; bandno++) {
226 opj_tcd_band_t *band = &res->bands[bandno];
227 opj_tcd_precinct_t *prc = &band->precincts[precno];
228 tgt_reset(prc->incltree);
229 tgt_reset(prc->imsbtree);
230 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
231 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
233 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
239 bio_init_enc(bio, c, length);
240 bio_write(bio, 1, 1); /* Empty header bit */
242 /* Writing Packet header */
243 for (bandno = 0; bandno < res->numbands; bandno++) {
244 opj_tcd_band_t *band = &res->bands[bandno];
245 opj_tcd_precinct_t *prc = &band->precincts[precno];
246 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
247 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
248 opj_tcd_layer_t *layer = &cblk->layers[layno];
249 if (!cblk->numpasses && layer->numpasses) {
250 tgt_setvalue(prc->incltree, cblkno, layno);
253 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
254 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
255 opj_tcd_layer_t *layer = &cblk->layers[layno];
259 /* cblk inclusion bits */
260 if (!cblk->numpasses) {
261 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
263 bio_write(bio, layer->numpasses != 0, 1);
265 /* if cblk not included, go to the next cblk */
266 if (!layer->numpasses) {
269 /* if first instance of cblk --> zero bit-planes information */
270 if (!cblk->numpasses) {
271 cblk->numlenbits = 3;
272 tgt_encode(bio, prc->imsbtree, cblkno, 999);
274 /* number of coding passes included */
275 t2_putnumpasses(bio, layer->numpasses);
277 /* computation of the increase of the length indicator and insertion in the header */
278 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
279 opj_tcd_pass_t *pass = &cblk->passes[passno];
282 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
283 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
288 t2_putcommacode(bio, increment);
290 /* computation of the new Length indicator */
291 cblk->numlenbits += increment;
293 /* insertion of the codeword segment length */
294 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
295 opj_tcd_pass_t *pass = &cblk->passes[passno];
298 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
299 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
307 if (bio_flush(bio)) {
309 return -999; /* modified to eliminate longjmp !! */
312 c += bio_numbytes(bio);
316 if (tcp->csty & J2K_CP_CSTY_EPH) {
324 // End of packet header position. Currently only represents the distance to start of packet
325 // Will be updated later by incrementing with packet start value
326 if(cstr_info && cstr_info->index_write) {
327 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
328 info_PK->end_ph_pos = (int)(c - dest);
332 /* Writing the packet body */
334 for (bandno = 0; bandno < res->numbands; bandno++) {
335 opj_tcd_band_t *band = &res->bands[bandno];
336 opj_tcd_precinct_t *prc = &band->precincts[precno];
337 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
338 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
339 opj_tcd_layer_t *layer = &cblk->layers[layno];
340 if (!layer->numpasses) {
343 if (c + layer->len > dest + length) {
347 memcpy(c, layer->data, layer->len);
348 cblk->numpasses += layer->numpasses;
351 if(cstr_info && cstr_info->index_write) {
352 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
353 info_PK->disto += layer->disto;
354 if (cstr_info->D_max < info_PK->disto) {
355 cstr_info->D_max = info_PK->disto;
365 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) {
367 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
368 seg = &cblk->segs[index];
373 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
376 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
380 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
383 seg->maxpasses = 109;
387 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
388 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
390 unsigned char *c = src;
392 opj_cp_t *cp = t2->cp;
394 int compno = pi->compno; /* component value */
395 int resno = pi->resno; /* resolution level value */
396 int precno = pi->precno; /* precinct value */
397 int layno = pi->layno; /* quality layer value */
399 opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno];
401 unsigned char *hd = NULL;
404 opj_bio_t *bio = NULL; /* BIO component */
407 for (bandno = 0; bandno < res->numbands; bandno++) {
408 opj_tcd_band_t *band = &res->bands[bandno];
409 opj_tcd_precinct_t *prc = &band->precincts[precno];
411 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
413 tgt_reset(prc->incltree);
414 tgt_reset(prc->imsbtree);
415 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
416 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
424 if (tcp->csty & J2K_CP_CSTY_SOP) {
425 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
426 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
431 /** TODO : check the Nsop value */
435 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
436 This part deal with this caracteristic
437 step 1: Read packet header in the saved structure
438 step 2: Return to codestream for decoding
443 if (cp->ppm == 1) { /* PPM */
445 bio_init_dec(bio, hd, cp->ppm_len);
446 } else if (tcp->ppt == 1) { /* PPT */
448 bio_init_dec(bio, hd, tcp->ppt_len);
449 } else { /* Normal Case */
451 bio_init_dec(bio, hd, src+len-hd);
454 present = bio_read(bio, 1);
458 hd += bio_numbytes(bio);
463 if (tcp->csty & J2K_CP_CSTY_EPH) {
464 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
465 printf("Error : expected EPH marker\n");
472 // End of packet header position. Currently only represents the distance to start of packet
473 // Will be updated later by incrementing with packet start value
475 pack_info->end_ph_pos = (int)(c - src);
479 if (cp->ppm == 1) { /* PPM case */
480 cp->ppm_len += cp->ppm_data-hd;
484 if (tcp->ppt == 1) { /* PPT case */
485 tcp->ppt_len+=tcp->ppt_data-hd;
493 for (bandno = 0; bandno < res->numbands; bandno++) {
494 opj_tcd_band_t *band = &res->bands[bandno];
495 opj_tcd_precinct_t *prc = &band->precincts[precno];
497 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
499 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
500 int included, increment, n, segno;
501 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
502 /* if cblk not yet included before --> inclusion tagtree */
503 if (!cblk->numsegs) {
504 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
507 included = bio_read(bio, 1);
509 /* if cblk not included */
511 cblk->numnewpasses = 0;
514 /* if cblk not yet included --> zero-bitplane tagtree */
515 if (!cblk->numsegs) {
517 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
521 cblk->numbps = band->numbps - numimsbs;
522 cblk->numlenbits = 3;
524 /* number of coding passes */
525 cblk->numnewpasses = t2_getnumpasses(bio);
526 increment = t2_getcommacode(bio);
527 /* length indicator increment */
528 cblk->numlenbits += increment;
530 if (!cblk->numsegs) {
531 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1);
533 segno = cblk->numsegs - 1;
534 if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
536 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
539 n = cblk->numnewpasses;
542 cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
543 cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
544 n -= cblk->segs[segno].numnewpasses;
547 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
553 if (bio_inalign(bio)) {
558 hd += bio_numbytes(bio);
562 if (tcp->csty & J2K_CP_CSTY_EPH) {
563 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
564 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
572 // End of packet header position. Currently only represents the distance to start of packet
573 // Will be updated later by incrementing with packet start value
575 pack_info->end_ph_pos = (int)(hd - src);
580 cp->ppm_len+=cp->ppm_data-hd;
582 } else if (tcp->ppt == 1) {
583 tcp->ppt_len+=tcp->ppt_data-hd;
589 for (bandno = 0; bandno < res->numbands; bandno++) {
590 opj_tcd_band_t *band = &res->bands[bandno];
591 opj_tcd_precinct_t *prc = &band->precincts[precno];
593 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
595 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
596 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
597 opj_tcd_seg_t *seg = NULL;
598 if (!cblk->numnewpasses)
600 if (!cblk->numsegs) {
601 seg = &cblk->segs[0];
605 seg = &cblk->segs[cblk->numsegs - 1];
606 if (seg->numpasses == seg->maxpasses) {
613 if (c + seg->newlen > src + len) {
618 /* we need here a j2k handle to verify if making a check to
619 the validity of cblocks parameters is selected from user (-W) */
621 /* let's check that we are not exceeding */
622 if ((cblk->len + seg->newlen) > 8192) {
623 opj_event_msg(t2->cinfo, EVT_WARNING,
624 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
625 seg->newlen, cblkno, precno, bandno, resno, compno);
627 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
630 seg->newlen = 8192 - cblk->len;
631 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
635 #endif /* USE_JPWL */
637 cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char*));
638 memcpy(cblk->data + cblk->len, c, seg->newlen);
639 if (seg->numpasses == 0) {
640 seg->data = &cblk->data;
641 seg->dataindex = cblk->len;
644 cblk->len += seg->newlen;
645 seg->len += seg->newlen;
646 seg->numpasses += seg->numnewpasses;
647 cblk->numnewpasses -= seg->numnewpasses;
648 if (cblk->numnewpasses > 0) {
652 } while (cblk->numnewpasses > 0);
659 /* ----------------------------------------------------------------------- */
661 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){
662 unsigned char *c = dest;
665 opj_pi_iterator_t *pi = NULL;
667 opj_image_t *image = t2->image;
668 opj_cp_t *cp = t2->cp;
669 opj_tcp_t *tcp = &cp->tcps[tileno];
670 int pocno = cp->cinema == CINEMA4K_24? 2: 1;
671 int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
673 pi = pi_initialise_encode(image, cp, tileno, t2_mode);
675 /* TODO: throw an error */
679 if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
680 for(compno = 0; compno < maxcomp; compno++ ){
681 for(poc = 0; poc < pocno ; poc++){
684 if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
685 opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
686 pi_destroy(pi, cp, tileno);
689 while (pi_next(&pi[poc])) {
690 if (pi[poc].layno < maxlayers) {
691 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
692 comp_len = comp_len + e;
700 if (e == -999) break;
701 if (cp->max_comp_size){
702 if (comp_len > cp->max_comp_size){
708 if (e == -999) break;
710 }else{ /* t2_mode == FINAL_PASS */
711 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
712 while (pi_next(&pi[pino])) {
713 if (pi[pino].layno < maxlayers) {
714 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
722 if(cstr_info->index_write) {
723 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
724 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
725 if (!cstr_info->packno) {
726 info_PK->start_pos = info_TL->end_header + 1;
728 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;
730 info_PK->end_pos = info_PK->start_pos + e - 1;
731 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
732 // to start of packet is incremented by value of start of packet
743 pi_destroy(pi, cp, tileno);
752 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) {
753 unsigned char *c = src;
754 opj_pi_iterator_t *pi;
756 int n = 0, curtp = 0;
759 opj_image_t *image = t2->image;
760 opj_cp_t *cp = t2->cp;
762 /* create a packet iterator */
763 pi = pi_create_decode(image, cp, tileno);
765 /* TODO: throw an error */
771 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
772 while (pi_next(&pi[pino])) {
773 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
774 opj_packet_info_t *pack_info;
776 pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
779 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
783 if(e == -999) return -999;
784 /* progression in resolution */
785 image->comps[pi[pino].compno].resno_decoded =
787 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
788 : image->comps[pi[pino].compno].resno_decoded;
793 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
794 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
795 if (!cstr_info->packno) {
796 info_PK->start_pos = info_TL->end_header + 1;
797 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ // New tile part
798 info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
799 info_TL->tp[curtp].tp_start_pack = tp_start_packno;
800 tp_start_packno = cstr_info->packno;
802 info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
804 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;
806 info_PK->end_pos = info_PK->start_pos + e - 1;
807 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
808 // to start of packet is incremented by value of start of packet
813 if (e == -999) { /* ADD */
822 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in last tile-part
823 cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno;
827 /* don't forget to release pi */
828 pi_destroy(pi, cp, tileno);
837 opj_bool t2_decode_packets_v2(
839 OPJ_UINT32 p_tile_no,
840 struct opj_tcd_tile *p_tile,
842 OPJ_UINT32 * p_data_read,
843 OPJ_UINT32 p_max_len,
844 struct opj_codestream_info_v2 *p_cstr_info)
846 OPJ_BYTE *l_current_data = p_src;
847 opj_pi_iterator_t *l_pi = 00;
849 opj_image_header_t *l_image = p_t2->image;
850 opj_cp_v2_t *l_cp = p_t2->cp;
851 opj_cp_v2_t *cp = p_t2->cp;
852 opj_tcp_v2_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
853 OPJ_UINT32 l_nb_bytes_read;
854 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
855 opj_pi_iterator_t *l_current_pi = 00;
856 OPJ_UINT32 curtp = 0;
857 OPJ_UINT32 tp_start_packno;
858 opj_packet_info_t *l_pack_info = 00;
859 opj_image_comp_header_t* l_img_comp = 00;
863 l_pack_info = p_cstr_info->tile[p_tile_no].packet;
866 /* create a packet iterator */
867 l_pi = pi_create_decode_v2(l_image, l_cp, p_tile_no);
875 for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
877 while (pi_next(l_current_pi)) {
879 if (l_tcp->num_layers_to_decode > l_current_pi->layno
880 && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
883 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)) {
884 pi_destroy_v2(l_pi,l_nb_pocs);
888 l_img_comp = &(l_image->comps[l_current_pi->compno]);
889 l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
893 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)) {
894 pi_destroy_v2(l_pi,l_nb_pocs);
899 l_current_data += l_nb_bytes_read;
900 p_max_len -= l_nb_bytes_read;
904 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
905 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
906 if (!p_cstr_info->packno) {
907 info_PK->start_pos = info_TL->end_header + 1;
908 } 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
909 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
910 tp_start_packno = p_cstr_info->packno;
912 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
914 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;
916 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
917 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
918 ++p_cstr_info->packno;
927 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
931 /* don't forget to release pi */
932 pi_destroy_v2(l_pi,l_nb_pocs);
933 *p_data_read = l_current_data - p_src;
937 /* ----------------------------------------------------------------------- */
939 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
940 /* create the tcd structure */
941 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
951 * Creates a Tier 2 handle
953 * @param p_image Source or destination image
954 * @param p_cp Image coding parameters.
955 * @return a new T2 handle if successful, NULL otherwise.
957 opj_t2_v2_t* t2_create_v2(
958 opj_image_header_t *p_image,
961 /* create the tcd structure */
962 opj_t2_v2_t *l_t2 = (opj_t2_v2_t*)opj_malloc(sizeof(opj_t2_v2_t));
968 memset(l_t2,0,sizeof(opj_t2_t));
969 l_t2->image = p_image;
974 void t2_destroy(opj_t2_t *t2) {
980 void t2_destroy_v2(opj_t2_v2_t *t2) {
987 static opj_bool t2_decode_packet_v2(
989 opj_tcd_tile_t *p_tile,
991 opj_pi_iterator_t *p_pi,
993 OPJ_UINT32 * p_data_read,
994 OPJ_UINT32 p_max_length,
995 opj_packet_info_t *p_pack_info)
997 opj_bool l_read_data;
998 OPJ_UINT32 l_nb_bytes_read = 0;
999 OPJ_UINT32 l_nb_total_bytes_read = 0;
1003 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)) {
1007 p_src += l_nb_bytes_read;
1008 l_nb_total_bytes_read += l_nb_bytes_read;
1009 p_max_length -= l_nb_bytes_read;
1011 /* we should read data for the packet */
1013 l_nb_bytes_read = 0;
1015 if (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
1019 l_nb_total_bytes_read += l_nb_bytes_read;
1022 *p_data_read = l_nb_total_bytes_read;
1027 static opj_bool t2_skip_packet(
1029 opj_tcd_tile_t *p_tile,
1030 opj_tcp_v2_t *p_tcp,
1031 opj_pi_iterator_t *p_pi,
1033 OPJ_UINT32 * p_data_read,
1034 OPJ_UINT32 p_max_length,
1035 opj_packet_info_t *p_pack_info)
1037 opj_bool l_read_data;
1038 OPJ_UINT32 l_nb_bytes_read = 0;
1039 OPJ_UINT32 l_nb_total_bytes_read = 0;
1044 (! 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))
1048 p_src += l_nb_bytes_read;
1049 l_nb_total_bytes_read += l_nb_bytes_read;
1050 p_max_length -= l_nb_bytes_read;
1051 /* we should read data for the packet */
1055 l_nb_bytes_read = 0;
1057 (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info))
1061 l_nb_total_bytes_read += l_nb_bytes_read;
1063 *p_data_read = l_nb_total_bytes_read;
1069 static opj_bool t2_read_packet_header(
1071 opj_tcd_tile_t *p_tile,
1072 opj_tcp_v2_t *p_tcp,
1073 opj_pi_iterator_t *p_pi,
1074 opj_bool * p_is_data_present,
1075 OPJ_BYTE *p_src_data,
1076 OPJ_UINT32 * p_data_read,
1077 OPJ_UINT32 p_max_length,
1078 opj_packet_info_t *p_pack_info)
1081 OPJ_UINT32 bandno, cblkno;
1082 OPJ_UINT32 l_nb_code_blocks;
1083 OPJ_UINT32 l_remaining_length;
1084 OPJ_UINT32 l_header_length;
1085 OPJ_UINT32 * l_modified_length_ptr = 00;
1086 OPJ_BYTE *l_current_data = p_src_data;
1087 opj_cp_v2_t *l_cp = p_t2->cp;
1088 opj_bio_t *l_bio = 00; /* BIO component */
1089 opj_tcd_band_t *l_band = 00;
1090 opj_tcd_cblk_dec_t* l_cblk = 00;
1091 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1093 OPJ_BYTE *l_header_data = 00;
1094 OPJ_BYTE **l_header_data_start = 00;
1096 OPJ_UINT32 l_present;
1101 l_band = l_res->bands;
1102 /* reset tagtrees */
1104 (bandno = 0; bandno < l_res->numbands; ++bandno)
1106 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1109 ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)))
1111 tgt_reset(l_prc->incltree);
1112 tgt_reset(l_prc->imsbtree);
1113 l_cblk = l_prc->cblks.dec;
1114 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1116 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
1118 l_cblk->numsegs = 0;
1119 l_cblk->real_num_segs = 0;
1129 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
1130 if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
1131 // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n");
1133 l_current_data += 6;
1136 /** TODO : check the Nsop value */
1140 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
1141 This part deal with this caracteristic
1142 step 1: Read packet header in the saved structure
1143 step 2: Return to codestream for decoding
1146 l_bio = bio_create();
1156 l_header_data_start = &l_cp->ppm_data;
1157 l_header_data = *l_header_data_start;
1158 l_modified_length_ptr = &(l_cp->ppm_len);
1164 l_header_data_start = &(p_tcp->ppt_data);
1165 l_header_data = *l_header_data_start;
1166 l_modified_length_ptr = &(p_tcp->ppt_len);
1170 l_header_data_start = &(l_current_data);
1171 l_header_data = *l_header_data_start;
1172 l_remaining_length = p_src_data+p_max_length-l_header_data;
1173 l_modified_length_ptr = &(l_remaining_length);
1175 bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
1176 l_present = bio_read(l_bio, 1);
1181 l_header_data += bio_numbytes(l_bio);
1184 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1185 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1186 printf("Error : expected EPH marker\n");
1191 l_header_length = (l_header_data - *l_header_data_start);
1192 *l_modified_length_ptr -= l_header_length;
1193 *l_header_data_start += l_header_length;
1195 // End of packet header position. Currently only represents the distance to start of packet
1196 // Will be updated later by incrementing with packet start value
1200 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1203 * p_is_data_present = OPJ_FALSE;
1204 *p_data_read = l_current_data - p_src_data;
1208 l_band = l_res->bands;
1210 (bandno = 0; bandno < l_res->numbands; ++bandno)
1212 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
1214 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
1219 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1220 l_cblk = l_prc->cblks.dec;
1222 (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++)
1224 OPJ_UINT32 l_included,l_increment, l_segno;
1226 /* if cblk not yet included before --> inclusion tagtree */
1230 l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1);
1235 l_included = bio_read(l_bio, 1);
1237 /* if cblk not included */
1241 l_cblk->numnewpasses = 0;
1245 /* if cblk not yet included --> zero-bitplane tagtree */
1251 (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i))
1255 l_cblk->numbps = l_band->numbps + 1 - i;
1256 l_cblk->numlenbits = 3;
1258 /* number of coding passes */
1259 l_cblk->numnewpasses = t2_getnumpasses(l_bio);
1260 l_increment = t2_getcommacode(l_bio);
1261 /* length indicator increment */
1262 l_cblk->numlenbits += l_increment;
1268 (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1))
1277 l_segno = l_cblk->numsegs - 1;
1279 (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses)
1283 (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0))
1290 n = l_cblk->numnewpasses;
1293 l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n);
1294 l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1295 n -= l_cblk->segs[l_segno].numnewpasses;
1301 (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0))
1315 (bio_inalign(l_bio))
1321 l_header_data += bio_numbytes(l_bio);
1325 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1326 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1327 // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n");
1334 l_header_length = (l_header_data - *l_header_data_start);
1335 *l_modified_length_ptr -= l_header_length;
1336 *l_header_data_start += l_header_length;
1338 // End of packet header position. Currently only represents the distance to start of packet
1339 // Will be updated later by incrementing with packet start value
1343 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1346 * p_is_data_present = OPJ_TRUE;
1347 *p_data_read = l_current_data - p_src_data;
1351 static opj_bool t2_read_packet_data(
1353 opj_tcd_tile_t *p_tile,
1354 opj_pi_iterator_t *p_pi,
1355 OPJ_BYTE *p_src_data,
1356 OPJ_UINT32 * p_data_read,
1357 OPJ_UINT32 p_max_length,
1358 opj_packet_info_t *pack_info)
1360 OPJ_UINT32 bandno, cblkno;
1361 OPJ_UINT32 l_nb_code_blocks;
1362 OPJ_BYTE *l_current_data = p_src_data;
1363 opj_tcd_band_t *l_band = 00;
1364 opj_tcd_cblk_dec_t* l_cblk = 00;
1365 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1367 l_band = l_res->bands;
1369 (bandno = 0; bandno < l_res->numbands; ++bandno)
1371 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1374 ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
1379 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1380 l_cblk = l_prc->cblks.dec;
1382 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
1384 opj_tcd_seg_t *l_seg = 00;
1386 (!l_cblk->numnewpasses)
1395 l_seg = l_cblk->segs;
1401 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1403 (l_seg->numpasses == l_seg->maxpasses)
1413 (l_current_data + l_seg->newlen > p_src_data + p_max_length)
1419 /* we need here a j2k handle to verify if making a check to
1420 the validity of cblocks parameters is selected from user (-W) */
1422 /* let's check that we are not exceeding */
1423 if ((cblk->len + seg->newlen) > 8192) {
1424 opj_event_msg(t2->cinfo, EVT_WARNING,
1425 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1426 seg->newlen, cblkno, precno, bandno, resno, compno);
1428 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1431 seg->newlen = 8192 - cblk->len;
1432 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
1436 #endif /* USE_JPWL */
1438 memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen);
1440 (l_seg->numpasses == 0)
1442 l_seg->data = &l_cblk->data;
1443 l_seg->dataindex = l_cblk->len;
1445 l_current_data += l_seg->newlen;
1446 l_seg->numpasses += l_seg->numnewpasses;
1447 l_cblk->numnewpasses -= l_seg->numnewpasses;
1449 l_seg->real_num_passes = l_seg->numpasses;
1450 l_cblk->len += l_seg->newlen;
1451 l_seg->len += l_seg->newlen;
1453 (l_cblk->numnewpasses > 0)
1459 while (l_cblk->numnewpasses > 0);
1460 l_cblk->real_num_segs = l_cblk->numsegs;
1465 *(p_data_read) = l_current_data - p_src_data;
1469 static opj_bool t2_skip_packet_data(
1471 opj_tcd_tile_t *p_tile,
1472 opj_pi_iterator_t *p_pi,
1473 OPJ_UINT32 * p_data_read,
1474 OPJ_UINT32 p_max_length,
1475 opj_packet_info_t *pack_info)
1477 OPJ_UINT32 bandno, cblkno;
1478 OPJ_UINT32 l_nb_code_blocks;
1479 opj_tcd_band_t *l_band = 00;
1480 opj_tcd_cblk_dec_t* l_cblk = 00;
1482 opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1485 l_band = l_res->bands;
1487 (bandno = 0; bandno < l_res->numbands; ++bandno)
1489 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1492 ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))
1497 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1498 l_cblk = l_prc->cblks.dec;
1500 (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)
1502 opj_tcd_seg_t *l_seg = 00;
1504 (!l_cblk->numnewpasses)
1513 l_seg = l_cblk->segs;
1519 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1521 (l_seg->numpasses == l_seg->maxpasses)
1531 (* p_data_read + l_seg->newlen > p_max_length)
1537 /* we need here a j2k handle to verify if making a check to
1538 the validity of cblocks parameters is selected from user (-W) */
1540 /* let's check that we are not exceeding */
1541 if ((cblk->len + seg->newlen) > 8192) {
1542 opj_event_msg(t2->cinfo, EVT_WARNING,
1543 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1544 seg->newlen, cblkno, precno, bandno, resno, compno);
1546 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1549 seg->newlen = 8192 - cblk->len;
1550 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
1554 #endif /* USE_JPWL */
1555 *(p_data_read) += l_seg->newlen;
1556 l_seg->numpasses += l_seg->numnewpasses;
1557 l_cblk->numnewpasses -= l_seg->numnewpasses;
1559 (l_cblk->numnewpasses > 0)
1565 while (l_cblk->numnewpasses > 0);
1574 static opj_bool t2_init_seg_v2(opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first)
1576 opj_tcd_seg_t* seg = 00;
1577 OPJ_UINT32 l_nb_segs = index + 1;
1580 (l_nb_segs > cblk->m_current_max_segs)
1582 cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS;
1583 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1590 seg = &cblk->segs[index];
1591 memset(seg,0,sizeof(opj_tcd_seg_t));
1593 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1596 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1598 seg->maxpasses = 10;
1600 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1603 seg->maxpasses = 109;