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);
63 Encode a packet of a tile to a destination buffer
64 @param tile Tile for which to write the packets
65 @param tcp Tile coding parameters
66 @param pi Packet identity
67 @param dest Destination buffer
68 @param len Length of the destination buffer
69 @param cstr_info Codestream information structure
70 @param tileno Number of the tile encoded
73 static opj_bool t2_encode_packet_v2(
75 opj_tcd_tile_v2_t *tile,
77 opj_pi_iterator_t *pi,
79 OPJ_UINT32 * p_data_written,
81 opj_codestream_info_t *cstr_info);
89 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first);
91 Decode a packet of a tile from a source buffer
93 @param src Source buffer
94 @param len Length of the source buffer
95 @param tile Tile for which to write the packets
96 @param tcp Tile coding parameters
97 @param pi Packet identity
98 @param pack_info Packet information
101 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
102 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info);
106 Decode a packet of a tile from a source buffer
108 @param tile Tile for which to write the packets
109 @param tcp Tile coding parameters
110 @param pi Packet identity
111 @param src Source buffer
112 @param pack_info Packet information
116 static opj_bool t2_decode_packet_v2(
118 opj_tcd_tile_v2_t *tile,
120 opj_pi_iterator_t *pi,
122 OPJ_UINT32 * data_read,
123 OPJ_UINT32 max_length,
124 opj_packet_info_t *pack_info);
126 static opj_bool t2_skip_packet(
128 opj_tcd_tile_v2_t *p_tile,
130 opj_pi_iterator_t *p_pi,
132 OPJ_UINT32 * p_data_read,
133 OPJ_UINT32 p_max_length,
134 opj_packet_info_t *p_pack_info);
136 static opj_bool t2_read_packet_header(
138 opj_tcd_tile_v2_t *p_tile,
140 opj_pi_iterator_t *p_pi,
141 opj_bool * p_is_data_present,
142 OPJ_BYTE *p_src_data,
143 OPJ_UINT32 * p_data_read,
144 OPJ_UINT32 p_max_length,
145 opj_packet_info_t *p_pack_info);
147 static opj_bool t2_read_packet_data(
149 opj_tcd_tile_v2_t *p_tile,
150 opj_pi_iterator_t *p_pi,
151 OPJ_BYTE *p_src_data,
152 OPJ_UINT32 * p_data_read,
153 OPJ_UINT32 p_max_length,
154 opj_packet_info_t *pack_info);
156 static opj_bool t2_skip_packet_data(
158 opj_tcd_tile_v2_t *p_tile,
159 opj_pi_iterator_t *p_pi,
160 OPJ_UINT32 * p_data_read,
161 OPJ_UINT32 p_max_length,
162 opj_packet_info_t *pack_info);
170 static opj_bool t2_init_seg_v2( opj_tcd_cblk_dec_v2_t* cblk,
179 /* ----------------------------------------------------------------------- */
181 /* #define RESTART 0x04 */
183 static void t2_putcommacode(opj_bio_t *bio, int n) {
185 bio_write(bio, 1, 1);
187 bio_write(bio, 0, 1);
190 static int t2_getcommacode(opj_bio_t *bio) {
192 for (n = 0; bio_read(bio, 1); n++) {
198 static void t2_putnumpasses(opj_bio_t *bio, int n) {
200 bio_write(bio, 0, 1);
202 bio_write(bio, 2, 2);
204 bio_write(bio, 0xc | (n - 3), 4);
205 } else if (n <= 36) {
206 bio_write(bio, 0x1e0 | (n - 6), 9);
207 } else if (n <= 164) {
208 bio_write(bio, 0xff80 | (n - 37), 16);
212 static int t2_getnumpasses(opj_bio_t *bio) {
214 if (!bio_read(bio, 1))
216 if (!bio_read(bio, 1))
218 if ((n = bio_read(bio, 2)) != 3)
220 if ((n = bio_read(bio, 5)) != 31)
222 return (37 + bio_read(bio, 7));
225 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) {
227 unsigned char *c = dest;
229 int compno = pi->compno; /* component value */
230 int resno = pi->resno; /* resolution level value */
231 int precno = pi->precno; /* precinct value */
232 int layno = pi->layno; /* quality layer value */
234 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
235 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
237 opj_bio_t *bio = NULL; /* BIO component */
240 if (tcp->csty & J2K_CP_CSTY_SOP) {
245 c[4] = (unsigned char)((tile->packno % 65536) / 256);
246 c[5] = (unsigned char)((tile->packno % 65536) % 256);
252 for (bandno = 0; bandno < res->numbands; bandno++) {
253 opj_tcd_band_t *band = &res->bands[bandno];
254 opj_tcd_precinct_t *prc = &band->precincts[precno];
255 tgt_reset(prc->incltree);
256 tgt_reset(prc->imsbtree);
257 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
258 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
260 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
266 bio_init_enc(bio, c, length);
267 bio_write(bio, 1, 1); /* Empty header bit */
269 /* Writing Packet header */
270 for (bandno = 0; bandno < res->numbands; bandno++) {
271 opj_tcd_band_t *band = &res->bands[bandno];
272 opj_tcd_precinct_t *prc = &band->precincts[precno];
273 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
274 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
275 opj_tcd_layer_t *layer = &cblk->layers[layno];
276 if (!cblk->numpasses && layer->numpasses) {
277 tgt_setvalue(prc->incltree, cblkno, layno);
280 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
281 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
282 opj_tcd_layer_t *layer = &cblk->layers[layno];
286 /* cblk inclusion bits */
287 if (!cblk->numpasses) {
288 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
290 bio_write(bio, layer->numpasses != 0, 1);
292 /* if cblk not included, go to the next cblk */
293 if (!layer->numpasses) {
296 /* if first instance of cblk --> zero bit-planes information */
297 if (!cblk->numpasses) {
298 cblk->numlenbits = 3;
299 tgt_encode(bio, prc->imsbtree, cblkno, 999);
301 /* number of coding passes included */
302 t2_putnumpasses(bio, layer->numpasses);
304 /* computation of the increase of the length indicator and insertion in the header */
305 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
306 opj_tcd_pass_t *pass = &cblk->passes[passno];
309 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
310 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
315 t2_putcommacode(bio, increment);
317 /* computation of the new Length indicator */
318 cblk->numlenbits += increment;
320 /* insertion of the codeword segment length */
321 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
322 opj_tcd_pass_t *pass = &cblk->passes[passno];
325 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
326 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
334 if (bio_flush(bio)) {
336 return -999; /* modified to eliminate longjmp !! */
339 c += bio_numbytes(bio);
343 if (tcp->csty & J2K_CP_CSTY_EPH) {
351 /* End of packet header position. Currently only represents the distance to start of packet
352 // Will be updated later by incrementing with packet start value */
353 if(cstr_info && cstr_info->index_write) {
354 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
355 info_PK->end_ph_pos = (int)(c - dest);
359 /* Writing the packet body */
361 for (bandno = 0; bandno < res->numbands; bandno++) {
362 opj_tcd_band_t *band = &res->bands[bandno];
363 opj_tcd_precinct_t *prc = &band->precincts[precno];
364 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
365 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
366 opj_tcd_layer_t *layer = &cblk->layers[layno];
367 if (!layer->numpasses) {
370 if (c + layer->len > dest + length) {
374 memcpy(c, layer->data, layer->len);
375 cblk->numpasses += layer->numpasses;
378 if(cstr_info && cstr_info->index_write) {
379 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
380 info_PK->disto += layer->disto;
381 if (cstr_info->D_max < info_PK->disto) {
382 cstr_info->D_max = info_PK->disto;
392 static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) {
394 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
395 seg = &cblk->segs[index];
400 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
403 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
407 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
410 seg->maxpasses = 109;
414 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
415 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
417 unsigned char *c = src;
419 opj_cp_t *cp = t2->cp;
421 int compno = pi->compno; /* component value */
422 int resno = pi->resno; /* resolution level value */
423 int precno = pi->precno; /* precinct value */
424 int layno = pi->layno; /* quality layer value */
426 opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno];
428 unsigned char *hd = NULL;
431 opj_bio_t *bio = NULL; /* BIO component */
434 for (bandno = 0; bandno < res->numbands; bandno++) {
435 opj_tcd_band_t *band = &res->bands[bandno];
436 opj_tcd_precinct_t *prc = &band->precincts[precno];
438 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
440 tgt_reset(prc->incltree);
441 tgt_reset(prc->imsbtree);
442 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
443 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
451 if (tcp->csty & J2K_CP_CSTY_SOP) {
452 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
453 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
458 /** TODO : check the Nsop value */
462 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
463 This part deal with this caracteristic
464 step 1: Read packet header in the saved structure
465 step 2: Return to codestream for decoding
470 if (cp->ppm == 1) { /* PPM */
472 bio_init_dec(bio, hd, cp->ppm_len);
473 } else if (tcp->ppt == 1) { /* PPT */
475 bio_init_dec(bio, hd, tcp->ppt_len);
476 } else { /* Normal Case */
478 bio_init_dec(bio, hd, src+len-hd);
481 present = bio_read(bio, 1);
485 hd += bio_numbytes(bio);
490 if (tcp->csty & J2K_CP_CSTY_EPH) {
491 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
492 printf("Error : expected EPH marker\n");
499 /* End of packet header position. Currently only represents the distance to start of packet
500 // Will be updated later by incrementing with packet start value*/
502 pack_info->end_ph_pos = (int)(c - src);
506 if (cp->ppm == 1) { /* PPM case */
507 cp->ppm_len += cp->ppm_data-hd;
511 if (tcp->ppt == 1) { /* PPT case */
512 tcp->ppt_len+=tcp->ppt_data-hd;
520 for (bandno = 0; bandno < res->numbands; bandno++) {
521 opj_tcd_band_t *band = &res->bands[bandno];
522 opj_tcd_precinct_t *prc = &band->precincts[precno];
524 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
526 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
527 int included, increment, n, segno;
528 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
529 /* if cblk not yet included before --> inclusion tagtree */
530 if (!cblk->numsegs) {
531 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
534 included = bio_read(bio, 1);
536 /* if cblk not included */
538 cblk->numnewpasses = 0;
541 /* if cblk not yet included --> zero-bitplane tagtree */
542 if (!cblk->numsegs) {
544 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
548 cblk->numbps = band->numbps - numimsbs;
549 cblk->numlenbits = 3;
551 /* number of coding passes */
552 cblk->numnewpasses = t2_getnumpasses(bio);
553 increment = t2_getcommacode(bio);
554 /* length indicator increment */
555 cblk->numlenbits += increment;
557 if (!cblk->numsegs) {
558 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1);
560 segno = cblk->numsegs - 1;
561 if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
563 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
566 n = cblk->numnewpasses;
569 cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
570 cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
571 n -= cblk->segs[segno].numnewpasses;
574 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
580 if (bio_inalign(bio)) {
585 hd += bio_numbytes(bio);
589 if (tcp->csty & J2K_CP_CSTY_EPH) {
590 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
591 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
599 /* End of packet header position. Currently only represents the distance to start of packet
600 // Will be updated later by incrementing with packet start value*/
602 pack_info->end_ph_pos = (int)(hd - src);
607 cp->ppm_len+=cp->ppm_data-hd;
609 } else if (tcp->ppt == 1) {
610 tcp->ppt_len+=tcp->ppt_data-hd;
616 for (bandno = 0; bandno < res->numbands; bandno++) {
617 opj_tcd_band_t *band = &res->bands[bandno];
618 opj_tcd_precinct_t *prc = &band->precincts[precno];
620 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
622 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
623 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
624 opj_tcd_seg_t *seg = NULL;
625 if (!cblk->numnewpasses)
627 if (!cblk->numsegs) {
628 seg = &cblk->segs[0];
632 seg = &cblk->segs[cblk->numsegs - 1];
633 if (seg->numpasses == seg->maxpasses) {
640 if (c + seg->newlen > src + len) {
645 /* we need here a j2k handle to verify if making a check to
646 the validity of cblocks parameters is selected from user (-W) */
648 /* let's check that we are not exceeding */
649 if ((cblk->len + seg->newlen) > 8192) {
650 opj_event_msg(t2->cinfo, EVT_WARNING,
651 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
652 seg->newlen, cblkno, precno, bandno, resno, compno);
654 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
657 seg->newlen = 8192 - cblk->len;
658 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
662 #endif /* USE_JPWL */
664 cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char));
665 memcpy(cblk->data + cblk->len, c, seg->newlen);
666 if (seg->numpasses == 0) {
667 seg->data = &cblk->data;
668 seg->dataindex = cblk->len;
671 cblk->len += seg->newlen;
672 seg->len += seg->newlen;
673 seg->numpasses += seg->numnewpasses;
674 cblk->numnewpasses -= seg->numnewpasses;
675 if (cblk->numnewpasses > 0) {
679 } while (cblk->numnewpasses > 0);
686 /* ----------------------------------------------------------------------- */
688 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){
689 unsigned char *c = dest;
692 opj_pi_iterator_t *pi = NULL;
694 opj_image_t *image = t2->image;
695 opj_cp_t *cp = t2->cp;
696 opj_tcp_t *tcp = &cp->tcps[tileno];
697 int pocno = cp->cinema == CINEMA4K_24? 2: 1;
698 int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
700 pi = pi_initialise_encode(image, cp, tileno, t2_mode);
702 /* TODO: throw an error */
706 if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
707 for(compno = 0; compno < maxcomp; compno++ ){
708 for(poc = 0; poc < pocno ; poc++){
711 if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
712 opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
713 pi_destroy(pi, cp, tileno);
716 while (pi_next(&pi[poc])) {
717 if (pi[poc].layno < maxlayers) {
718 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
719 comp_len = comp_len + e;
727 if (e == -999) break;
728 if (cp->max_comp_size){
729 if (comp_len > cp->max_comp_size){
735 if (e == -999) break;
737 }else{ /* t2_mode == FINAL_PASS */
738 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
739 while (pi_next(&pi[pino])) {
740 if (pi[pino].layno < maxlayers) {
741 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
749 if(cstr_info->index_write) {
750 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
751 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
752 if (!cstr_info->packno) {
753 info_PK->start_pos = info_TL->end_header + 1;
755 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;
757 info_PK->end_pos = info_PK->start_pos + e - 1;
758 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance
759 // to start of packet is incremented by value of start of packet*/
770 pi_destroy(pi, cp, tileno);
779 opj_bool t2_encode_packets_v2(
781 OPJ_UINT32 p_tile_no,
782 opj_tcd_tile_v2_t *p_tile,
783 OPJ_UINT32 p_maxlayers,
785 OPJ_UINT32 * p_data_written,
786 OPJ_UINT32 p_max_len,
787 opj_codestream_info_t *cstr_info,
791 J2K_T2_MODE p_t2_mode)
793 OPJ_BYTE *l_current_data = p_dest;
794 OPJ_UINT32 l_nb_bytes = 0;
797 opj_pi_iterator_t *l_pi = 00;
798 opj_pi_iterator_t *l_current_pi = 00;
799 opj_image_t *l_image = p_t2->image;
800 opj_cp_v2_t *l_cp = p_t2->cp;
801 opj_tcp_v2_t *l_tcp = &l_cp->tcps[p_tile_no];
802 OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == CINEMA4K_24? 2: 1;
803 OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
804 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
806 l_pi = pi_initialise_encode_v2(l_image, l_cp, p_tile_no, p_t2_mode);
811 * p_data_written = 0;
813 if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */
816 for (compno = 0; compno < l_max_comp; ++compno) {
817 OPJ_UINT32 l_comp_len = 0;
820 for (poc = 0; poc < pocno ; ++poc) {
821 OPJ_UINT32 l_tp_num = compno;
823 pi_create_encode_v2(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
825 while (pi_next(l_current_pi)) {
826 if (l_current_pi->layno < p_maxlayers) {
829 if (! t2_encode_packet_v2(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
830 pi_destroy_v2(l_pi, l_nb_pocs);
834 l_comp_len += l_nb_bytes;
835 l_current_data += l_nb_bytes;
836 p_max_len -= l_nb_bytes;
838 * p_data_written += l_nb_bytes;
842 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
843 if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
844 pi_destroy_v2(l_pi, l_nb_pocs);
853 else { /* t2_mode == FINAL_PASS */
854 pi_create_encode_v2(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
856 l_current_pi = &l_pi[p_pino];
858 while (pi_next(l_current_pi)) {
859 if (l_current_pi->layno < p_maxlayers) {
862 if (! t2_encode_packet_v2(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
863 pi_destroy_v2(l_pi, l_nb_pocs);
867 l_current_data += l_nb_bytes;
868 p_max_len -= l_nb_bytes;
870 * p_data_written += l_nb_bytes;
874 if(cstr_info->index_write) {
875 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
876 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
877 if (!cstr_info->packno) {
878 info_PK->start_pos = info_TL->end_header + 1;
880 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;
882 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
883 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
884 // to start of packet is incremented by value of start of packet
895 pi_destroy_v2(l_pi, l_nb_pocs);
901 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) {
902 unsigned char *c = src;
903 opj_pi_iterator_t *pi;
905 int n = 0, curtp = 0;
908 opj_image_t *image = t2->image;
909 opj_cp_t *cp = t2->cp;
911 /* create a packet iterator */
912 pi = pi_create_decode(image, cp, tileno);
914 /* TODO: throw an error */
920 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
921 while (pi_next(&pi[pino])) {
922 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
923 opj_packet_info_t *pack_info;
925 pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
928 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
932 if(e == -999) return -999;
933 /* progression in resolution */
934 image->comps[pi[pino].compno].resno_decoded =
936 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
937 : image->comps[pi[pino].compno].resno_decoded;
942 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
943 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
944 if (!cstr_info->packno) {
945 info_PK->start_pos = info_TL->end_header + 1;
946 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ /* New tile part*/
947 info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part*/
948 info_TL->tp[curtp].tp_start_pack = tp_start_packno;
949 tp_start_packno = cstr_info->packno;
951 info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
953 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;
955 info_PK->end_pos = info_PK->start_pos + e - 1;
956 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance
957 // to start of packet is incremented by value of start of packet*/
962 if (e == -999) { /* ADD */
971 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part*/
972 cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno;
976 /* don't forget to release pi */
977 pi_destroy(pi, cp, tileno);
986 opj_bool t2_decode_packets_v2(
988 OPJ_UINT32 p_tile_no,
989 struct opj_tcd_tile_v2 *p_tile,
991 OPJ_UINT32 * p_data_read,
992 OPJ_UINT32 p_max_len,
993 opj_codestream_index_t *p_cstr_index)
995 OPJ_BYTE *l_current_data = p_src;
996 opj_pi_iterator_t *l_pi = 00;
998 opj_image_t *l_image = p_t2->image;
999 opj_cp_v2_t *l_cp = p_t2->cp;
1000 opj_cp_v2_t *cp = p_t2->cp;
1001 opj_tcp_v2_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
1002 OPJ_UINT32 l_nb_bytes_read;
1003 OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
1004 opj_pi_iterator_t *l_current_pi = 00;
1005 OPJ_UINT32 curtp = 0;
1006 OPJ_UINT32 tp_start_packno;
1007 opj_packet_info_t *l_pack_info = 00;
1008 opj_image_comp_t* l_img_comp = 00;
1012 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
1016 /* create a packet iterator */
1017 l_pi = pi_create_decode_v2(l_image, l_cp, p_tile_no);
1022 tp_start_packno = 0;
1023 l_current_pi = l_pi;
1025 for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
1027 /* if the resolution needed is to low, one dim of the tilec could be equal to zero
1028 * and no packets are used to encode this resolution and
1029 * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
1030 * and no l_img_comp->resno_decoded are computed
1032 opj_bool* first_pass_failed = (opj_bool*)opj_malloc(l_image->numcomps * sizeof(opj_bool));
1033 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(opj_bool));
1035 while (pi_next(l_current_pi)) {
1038 if (l_tcp->num_layers_to_decode > l_current_pi->layno
1039 && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
1040 l_nb_bytes_read = 0;
1042 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
1044 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)) {
1045 pi_destroy_v2(l_pi,l_nb_pocs);
1049 l_img_comp = &(l_image->comps[l_current_pi->compno]);
1050 l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
1053 l_nb_bytes_read = 0;
1054 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)) {
1055 pi_destroy_v2(l_pi,l_nb_pocs);
1060 if (first_pass_failed[l_current_pi->compno]) {
1061 l_img_comp = &(l_image->comps[l_current_pi->compno]);
1062 if (l_img_comp->resno_decoded == 0)
1063 l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
1066 l_current_data += l_nb_bytes_read;
1067 p_max_len -= l_nb_bytes_read;
1072 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
1073 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
1074 if (!p_cstr_info->packno) {
1075 info_PK->start_pos = info_TL->end_header + 1;
1076 } 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 */
1077 info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
1078 tp_start_packno = p_cstr_info->packno;
1080 info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
1082 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;
1084 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
1085 info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance */
1086 ++p_cstr_info->packno;
1093 opj_free(first_pass_failed);
1099 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 */
1104 /* don't forget to release pi */
1105 pi_destroy_v2(l_pi,l_nb_pocs);
1106 *p_data_read = l_current_data - p_src;
1110 /* ----------------------------------------------------------------------- */
1112 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
1113 /* create the tcd structure */
1114 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
1115 if(!t2) return NULL;
1124 * Creates a Tier 2 handle
1126 * @param p_image Source or destination image
1127 * @param p_cp Image coding parameters.
1128 * @return a new T2 handle if successful, NULL otherwise.
1130 opj_t2_v2_t* t2_create_v2( opj_image_t *p_image,
1133 /* create the tcd structure */
1134 opj_t2_v2_t *l_t2 = (opj_t2_v2_t*)opj_malloc(sizeof(opj_t2_v2_t));
1138 memset(l_t2,0,sizeof(opj_t2_v2_t));
1140 l_t2->image = p_image;
1146 void t2_destroy(opj_t2_t *t2) {
1152 void t2_destroy_v2(opj_t2_v2_t *t2) {
1159 static opj_bool t2_decode_packet_v2(
1161 opj_tcd_tile_v2_t *p_tile,
1162 opj_tcp_v2_t *p_tcp,
1163 opj_pi_iterator_t *p_pi,
1165 OPJ_UINT32 * p_data_read,
1166 OPJ_UINT32 p_max_length,
1167 opj_packet_info_t *p_pack_info)
1169 opj_bool l_read_data;
1170 OPJ_UINT32 l_nb_bytes_read = 0;
1171 OPJ_UINT32 l_nb_total_bytes_read = 0;
1175 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)) {
1179 p_src += l_nb_bytes_read;
1180 l_nb_total_bytes_read += l_nb_bytes_read;
1181 p_max_length -= l_nb_bytes_read;
1183 /* we should read data for the packet */
1185 l_nb_bytes_read = 0;
1187 if (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
1191 l_nb_total_bytes_read += l_nb_bytes_read;
1194 *p_data_read = l_nb_total_bytes_read;
1199 static opj_bool t2_encode_packet_v2(
1201 opj_tcd_tile_v2_t * tile,
1203 opj_pi_iterator_t *pi,
1205 OPJ_UINT32 * p_data_written,
1207 opj_codestream_info_t *cstr_info)
1209 OPJ_UINT32 bandno, cblkno;
1211 OPJ_UINT32 l_nb_bytes;
1212 OPJ_UINT32 compno = pi->compno; /* component value */
1213 OPJ_UINT32 resno = pi->resno; /* resolution level value */
1214 OPJ_UINT32 precno = pi->precno; /* precinct value */
1215 OPJ_UINT32 layno = pi->layno; /* quality layer value */
1216 OPJ_UINT32 l_nb_blocks;
1217 opj_tcd_band_v2_t *band = 00;
1218 opj_tcd_cblk_enc_v2_t* cblk = 00;
1219 opj_tcd_pass_v2_t *pass = 00;
1221 opj_tcd_tilecomp_v2_t *tilec = &tile->comps[compno];
1222 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1224 opj_bio_t *bio = 00; /* BIO component */
1227 if (tcp->csty & J2K_CP_CSTY_SOP) {
1232 c[4] = (tile->packno % 65536) / 256;
1233 c[5] = (tile->packno % 65536) % 256;
1242 for(bandno = 0; bandno < res->numbands; ++bandno) {
1243 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1245 tgt_reset(prc->incltree);
1246 tgt_reset(prc->imsbtree);
1248 l_nb_blocks = prc->cw * prc->ch;
1249 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
1250 opj_tcd_cblk_enc_v2_t* cblk = &prc->cblks.enc[cblkno];
1252 cblk->numpasses = 0;
1253 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
1260 bio_init_enc(bio, c, length);
1261 bio_write(bio, 1, 1); /* Empty header bit */
1263 /* Writing Packet header */
1265 for (bandno = 0; bandno < res->numbands; ++bandno) {
1266 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1268 l_nb_blocks = prc->cw * prc->ch;
1269 cblk = prc->cblks.enc;
1271 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
1272 opj_tcd_layer_t *layer = &cblk->layers[layno];
1274 if (!cblk->numpasses && layer->numpasses) {
1275 tgt_setvalue(prc->incltree, cblkno, layno);
1281 cblk = prc->cblks.enc;
1282 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
1283 opj_tcd_layer_t *layer = &cblk->layers[layno];
1284 OPJ_UINT32 increment = 0;
1285 OPJ_UINT32 nump = 0;
1286 OPJ_UINT32 len = 0, passno;
1287 OPJ_UINT32 l_nb_passes;
1289 /* cblk inclusion bits */
1290 if (!cblk->numpasses) {
1291 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
1293 bio_write(bio, layer->numpasses != 0, 1);
1296 /* if cblk not included, go to the next cblk */
1297 if (!layer->numpasses) {
1302 /* if first instance of cblk --> zero bit-planes information */
1303 if (!cblk->numpasses) {
1304 cblk->numlenbits = 3;
1305 tgt_encode(bio, prc->imsbtree, cblkno, 999);
1308 /* number of coding passes included */
1309 t2_putnumpasses(bio, layer->numpasses);
1310 l_nb_passes = cblk->numpasses + layer->numpasses;
1311 pass = cblk->passes + cblk->numpasses;
1313 /* computation of the increase of the length indicator and insertion in the header */
1314 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
1318 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
1319 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
1326 t2_putcommacode(bio, increment);
1328 /* computation of the new Length indicator */
1329 cblk->numlenbits += increment;
1331 pass = cblk->passes + cblk->numpasses;
1332 /* insertion of the codeword segment length */
1333 for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
1337 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
1338 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
1351 if (bio_flush(bio)) {
1353 return OPJ_FALSE; /* modified to eliminate longjmp !! */
1356 l_nb_bytes = bio_numbytes(bio);
1358 length -= l_nb_bytes;
1363 if (tcp->csty & J2K_CP_CSTY_EPH) {
1372 // End of packet header position. Currently only represents the distance to start of packet
1373 // Will be updated later by incrementing with packet start value
1374 if(cstr_info && cstr_info->index_write) {
1375 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
1376 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
1380 /* Writing the packet body */
1382 for (bandno = 0; bandno < res->numbands; bandno++) {
1383 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1385 l_nb_blocks = prc->cw * prc->ch;
1386 cblk = prc->cblks.enc;
1388 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
1389 opj_tcd_layer_t *layer = &cblk->layers[layno];
1391 if (!layer->numpasses) {
1396 if (layer->len > length) {
1400 memcpy(c, layer->data, layer->len);
1401 cblk->numpasses += layer->numpasses;
1403 length -= layer->len;
1406 if(cstr_info && cstr_info->index_write) {
1407 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
1408 info_PK->disto += layer->disto;
1409 if (cstr_info->D_max < info_PK->disto) {
1410 cstr_info->D_max = info_PK->disto;
1420 * p_data_written += (c - dest);
1425 static opj_bool t2_skip_packet(
1427 opj_tcd_tile_v2_t *p_tile,
1428 opj_tcp_v2_t *p_tcp,
1429 opj_pi_iterator_t *p_pi,
1431 OPJ_UINT32 * p_data_read,
1432 OPJ_UINT32 p_max_length,
1433 opj_packet_info_t *p_pack_info)
1435 opj_bool l_read_data;
1436 OPJ_UINT32 l_nb_bytes_read = 0;
1437 OPJ_UINT32 l_nb_total_bytes_read = 0;
1441 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)) {
1445 p_src += l_nb_bytes_read;
1446 l_nb_total_bytes_read += l_nb_bytes_read;
1447 p_max_length -= l_nb_bytes_read;
1449 /* we should read data for the packet */
1451 l_nb_bytes_read = 0;
1453 if (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
1457 l_nb_total_bytes_read += l_nb_bytes_read;
1459 *p_data_read = l_nb_total_bytes_read;
1466 static opj_bool t2_read_packet_header(
1468 opj_tcd_tile_v2_t *p_tile,
1469 opj_tcp_v2_t *p_tcp,
1470 opj_pi_iterator_t *p_pi,
1471 opj_bool * p_is_data_present,
1472 OPJ_BYTE *p_src_data,
1473 OPJ_UINT32 * p_data_read,
1474 OPJ_UINT32 p_max_length,
1475 opj_packet_info_t *p_pack_info)
1478 OPJ_UINT32 bandno, cblkno;
1479 OPJ_UINT32 l_nb_code_blocks;
1480 OPJ_UINT32 l_remaining_length;
1481 OPJ_UINT32 l_header_length;
1482 OPJ_UINT32 * l_modified_length_ptr = 00;
1483 OPJ_BYTE *l_current_data = p_src_data;
1484 opj_cp_v2_t *l_cp = p_t2->cp;
1485 opj_bio_t *l_bio = 00; /* BIO component */
1486 opj_tcd_band_v2_t *l_band = 00;
1487 opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1488 opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1490 OPJ_BYTE *l_header_data = 00;
1491 OPJ_BYTE **l_header_data_start = 00;
1493 OPJ_UINT32 l_present;
1495 if (p_pi->layno == 0) {
1496 l_band = l_res->bands;
1498 /* reset tagtrees */
1499 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1500 opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1502 if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
1503 tgt_reset(l_prc->incltree);
1504 tgt_reset(l_prc->imsbtree);
1505 l_cblk = l_prc->cblks.dec;
1507 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1508 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1509 l_cblk->numsegs = 0;
1510 l_cblk->real_num_segs = 0;
1521 if (p_tcp->csty & J2K_CP_CSTY_SOP) {
1522 if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
1523 /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */
1525 l_current_data += 6;
1528 /** TODO : check the Nsop value */
1532 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
1533 This part deal with this caracteristic
1534 step 1: Read packet header in the saved structure
1535 step 2: Return to codestream for decoding
1538 l_bio = bio_create();
1543 if (l_cp->ppm == 1) { /* PPM */
1544 l_header_data_start = &l_cp->ppm_data;
1545 l_header_data = *l_header_data_start;
1546 l_modified_length_ptr = &(l_cp->ppm_len);
1549 else if (p_tcp->ppt == 1) { /* PPT */
1550 l_header_data_start = &(p_tcp->ppt_data);
1551 l_header_data = *l_header_data_start;
1552 l_modified_length_ptr = &(p_tcp->ppt_len);
1554 else { /* Normal Case */
1555 l_header_data_start = &(l_current_data);
1556 l_header_data = *l_header_data_start;
1557 l_remaining_length = p_src_data+p_max_length-l_header_data;
1558 l_modified_length_ptr = &(l_remaining_length);
1561 bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
1563 l_present = bio_read(l_bio, 1);
1566 l_header_data += bio_numbytes(l_bio);
1570 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1571 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1572 printf("Error : expected EPH marker\n");
1578 l_header_length = (l_header_data - *l_header_data_start);
1579 *l_modified_length_ptr -= l_header_length;
1580 *l_header_data_start += l_header_length;
1583 /* End of packet header position. Currently only represents the distance to start of packet
1584 Will be updated later by incrementing with packet start value */
1586 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1590 * p_is_data_present = OPJ_FALSE;
1591 *p_data_read = l_current_data - p_src_data;
1595 l_band = l_res->bands;
1596 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1597 opj_tcd_precinct_v2_t *l_prc = &(l_band->precincts[p_pi->precno]);
1599 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1604 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1605 l_cblk = l_prc->cblks.dec;
1606 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
1607 OPJ_UINT32 l_included,l_increment, l_segno;
1610 /* if cblk not yet included before --> inclusion tagtree */
1611 if (!l_cblk->numsegs) {
1612 l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1);
1616 l_included = bio_read(l_bio, 1);
1619 /* if cblk not included */
1621 l_cblk->numnewpasses = 0;
1626 /* if cblk not yet included --> zero-bitplane tagtree */
1627 if (!l_cblk->numsegs) {
1630 while (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i)) {
1634 l_cblk->numbps = l_band->numbps + 1 - i;
1635 l_cblk->numlenbits = 3;
1638 /* number of coding passes */
1639 l_cblk->numnewpasses = t2_getnumpasses(l_bio);
1640 l_increment = t2_getcommacode(l_bio);
1642 /* length indicator increment */
1643 l_cblk->numlenbits += l_increment;
1646 if (!l_cblk->numsegs) {
1647 if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1653 l_segno = l_cblk->numsegs - 1;
1654 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1656 if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1662 n = l_cblk->numnewpasses;
1665 l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n);
1666 l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1668 n -= l_cblk->segs[l_segno].numnewpasses;
1672 if (! t2_init_seg_v2(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1685 if (bio_inalign(l_bio)) {
1690 l_header_data += bio_numbytes(l_bio);
1694 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1695 if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1696 /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
1702 l_header_length = (l_header_data - *l_header_data_start);
1703 *l_modified_length_ptr -= l_header_length;
1704 *l_header_data_start += l_header_length;
1707 /* End of packet header position. Currently only represents the distance to start of packet
1708 Will be updated later by incrementing with packet start value */
1710 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1714 *p_is_data_present = OPJ_TRUE;
1715 *p_data_read = l_current_data - p_src_data;
1720 static opj_bool t2_read_packet_data(
1722 opj_tcd_tile_v2_t *p_tile,
1723 opj_pi_iterator_t *p_pi,
1724 OPJ_BYTE *p_src_data,
1725 OPJ_UINT32 * p_data_read,
1726 OPJ_UINT32 p_max_length,
1727 opj_packet_info_t *pack_info)
1729 OPJ_UINT32 bandno, cblkno;
1730 OPJ_UINT32 l_nb_code_blocks;
1731 OPJ_BYTE *l_current_data = p_src_data;
1732 opj_tcd_band_v2_t *l_band = 00;
1733 opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1734 opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1736 l_band = l_res->bands;
1737 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1738 opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1740 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1745 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1746 l_cblk = l_prc->cblks.dec;
1748 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1749 opj_tcd_seg_t *l_seg = 00;
1751 if (!l_cblk->numnewpasses) {
1757 if (!l_cblk->numsegs) {
1758 l_seg = l_cblk->segs;
1763 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1765 if (l_seg->numpasses == l_seg->maxpasses) {
1772 if (l_current_data + l_seg->newlen > p_src_data + p_max_length) {
1777 /* we need here a j2k handle to verify if making a check to
1778 the validity of cblocks parameters is selected from user (-W) */
1780 /* let's check that we are not exceeding */
1781 if ((l_cblk->len + l_seg->newlen) > 8192) {
1782 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1783 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1784 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1786 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1789 l_seg->newlen = 8192 - l_cblk->len;
1790 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1794 #endif /* USE_JPWL */
1796 memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen);
1798 if (l_seg->numpasses == 0) {
1799 l_seg->data = &l_cblk->data;
1800 l_seg->dataindex = l_cblk->len;
1803 l_current_data += l_seg->newlen;
1804 l_seg->numpasses += l_seg->numnewpasses;
1805 l_cblk->numnewpasses -= l_seg->numnewpasses;
1807 l_seg->real_num_passes = l_seg->numpasses;
1808 l_cblk->len += l_seg->newlen;
1809 l_seg->len += l_seg->newlen;
1811 if (l_cblk->numnewpasses > 0) {
1815 } while (l_cblk->numnewpasses > 0);
1817 l_cblk->real_num_segs = l_cblk->numsegs;
1824 *(p_data_read) = l_current_data - p_src_data;
1829 static opj_bool t2_skip_packet_data(
1831 opj_tcd_tile_v2_t *p_tile,
1832 opj_pi_iterator_t *p_pi,
1833 OPJ_UINT32 * p_data_read,
1834 OPJ_UINT32 p_max_length,
1835 opj_packet_info_t *pack_info)
1837 OPJ_UINT32 bandno, cblkno;
1838 OPJ_UINT32 l_nb_code_blocks;
1839 opj_tcd_band_v2_t *l_band = 00;
1840 opj_tcd_cblk_dec_v2_t* l_cblk = 00;
1841 opj_tcd_resolution_v2_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1844 l_band = l_res->bands;
1846 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1847 opj_tcd_precinct_v2_t *l_prc = &l_band->precincts[p_pi->precno];
1849 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1854 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1855 l_cblk = l_prc->cblks.dec;
1857 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1858 opj_tcd_seg_t *l_seg = 00;
1860 if (!l_cblk->numnewpasses) {
1866 if (!l_cblk->numsegs) {
1867 l_seg = l_cblk->segs;
1872 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1874 if (l_seg->numpasses == l_seg->maxpasses) {
1881 if (* p_data_read + l_seg->newlen > p_max_length) {
1886 /* we need here a j2k handle to verify if making a check to
1887 the validity of cblocks parameters is selected from user (-W) */
1889 /* let's check that we are not exceeding */
1890 if ((l_cblk->len + l_seg->newlen) > 8192) {
1891 opj_event_msg(p_t2->cinfo, EVT_WARNING,
1892 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1893 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1895 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1898 l_seg->newlen = 8192 - l_cblk->len;
1899 opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
1903 #endif /* USE_JPWL */
1904 *(p_data_read) += l_seg->newlen;
1906 l_seg->numpasses += l_seg->numnewpasses;
1907 l_cblk->numnewpasses -= l_seg->numnewpasses;
1908 if (l_cblk->numnewpasses > 0)
1913 } while (l_cblk->numnewpasses > 0);
1925 static opj_bool t2_init_seg_v2(opj_tcd_cblk_dec_v2_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first)
1927 opj_tcd_seg_t* seg = 00;
1928 OPJ_UINT32 l_nb_segs = index + 1;
1930 if (l_nb_segs > cblk->m_current_max_segs) {
1931 cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS;
1933 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1939 seg = &cblk->segs[index];
1940 memset(seg,0,sizeof(opj_tcd_seg_t));
1942 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1945 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1947 seg->maxpasses = 10;
1949 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1952 seg->maxpasses = 109;