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);
66 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
68 Decode a packet of a tile from a source buffer
70 @param src Source buffer
71 @param len Length of the source buffer
72 @param tile Tile for which to write the packets
73 @param tcp Tile coding parameters
74 @param pi Packet identity
77 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
78 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info);
84 /* ----------------------------------------------------------------------- */
86 /* #define RESTART 0x04 */
88 static void t2_putcommacode(opj_bio_t *bio, int n) {
95 static int t2_getcommacode(opj_bio_t *bio) {
97 for (n = 0; bio_read(bio, 1); n++) {
103 static void t2_putnumpasses(opj_bio_t *bio, int n) {
105 bio_write(bio, 0, 1);
107 bio_write(bio, 2, 2);
109 bio_write(bio, 0xc | (n - 3), 4);
110 } else if (n <= 36) {
111 bio_write(bio, 0x1e0 | (n - 6), 9);
112 } else if (n <= 164) {
113 bio_write(bio, 0xff80 | (n - 37), 16);
117 static int t2_getnumpasses(opj_bio_t *bio) {
119 if (!bio_read(bio, 1))
121 if (!bio_read(bio, 1))
123 if ((n = bio_read(bio, 2)) != 3)
125 if ((n = bio_read(bio, 5)) != 31)
127 return (37 + bio_read(bio, 7));
130 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) {
132 unsigned char *c = dest;
134 int compno = pi->compno; /* component value */
135 int resno = pi->resno; /* resolution level value */
136 int precno = pi->precno; /* precinct value */
137 int layno = pi->layno; /* quality layer value */
139 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
140 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
142 opj_bio_t *bio = NULL; /* BIO component */
145 if (tcp->csty & J2K_CP_CSTY_SOP) {
150 c[4] = (cstr_info->packno % 65536) / 256;
151 c[5] = (cstr_info->packno % 65536) % 256;
157 for (bandno = 0; bandno < res->numbands; bandno++) {
158 opj_tcd_band_t *band = &res->bands[bandno];
159 opj_tcd_precinct_t *prc = &band->precincts[precno];
160 tgt_reset(prc->incltree);
161 tgt_reset(prc->imsbtree);
162 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
163 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
165 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
171 bio_init_enc(bio, c, length);
172 bio_write(bio, 1, 1); /* Empty header bit */
174 /* Writing Packet header */
175 for (bandno = 0; bandno < res->numbands; bandno++) {
176 opj_tcd_band_t *band = &res->bands[bandno];
177 opj_tcd_precinct_t *prc = &band->precincts[precno];
178 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
179 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
180 opj_tcd_layer_t *layer = &cblk->layers[layno];
181 if (!cblk->numpasses && layer->numpasses) {
182 tgt_setvalue(prc->incltree, cblkno, layno);
185 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
186 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
187 opj_tcd_layer_t *layer = &cblk->layers[layno];
191 /* cblk inclusion bits */
192 if (!cblk->numpasses) {
193 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
195 bio_write(bio, layer->numpasses != 0, 1);
197 /* if cblk not included, go to the next cblk */
198 if (!layer->numpasses) {
201 /* if first instance of cblk --> zero bit-planes information */
202 if (!cblk->numpasses) {
203 cblk->numlenbits = 3;
204 tgt_encode(bio, prc->imsbtree, cblkno, 999);
206 /* number of coding passes included */
207 t2_putnumpasses(bio, layer->numpasses);
209 /* computation of the increase of the length indicator and insertion in the header */
210 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
211 opj_tcd_pass_t *pass = &cblk->passes[passno];
214 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
215 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
220 t2_putcommacode(bio, increment);
222 /* computation of the new Length indicator */
223 cblk->numlenbits += increment;
225 /* insertion of the codeword segment length */
226 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
227 opj_tcd_pass_t *pass = &cblk->passes[passno];
230 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
231 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
239 if (bio_flush(bio)) {
241 return -999; /* modified to eliminate longjmp !! */
244 c += bio_numbytes(bio);
248 if (tcp->csty & J2K_CP_CSTY_EPH) {
256 // End of packet header position. Currently only represents the distance to start of packet
257 // Will be updated later by incrementing with packet start value
258 if(cstr_info && cstr_info->index_write) {
259 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
260 info_PK->end_ph_pos = (int)(c - dest);
264 /* Writing the packet body */
266 for (bandno = 0; bandno < res->numbands; bandno++) {
267 opj_tcd_band_t *band = &res->bands[bandno];
268 opj_tcd_precinct_t *prc = &band->precincts[precno];
269 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
270 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
271 opj_tcd_layer_t *layer = &cblk->layers[layno];
272 if (!layer->numpasses) {
275 if (c + layer->len > dest + length) {
279 memcpy(c, layer->data, layer->len);
280 cblk->numpasses += layer->numpasses;
283 if(cstr_info && cstr_info->index_write) {
284 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
285 info_PK->disto += layer->disto;
286 if (cstr_info->D_max < info_PK->disto) {
287 cstr_info->D_max = info_PK->disto;
297 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
300 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
303 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
307 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
310 seg->maxpasses = 109;
314 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
315 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
317 unsigned char *c = src;
319 opj_cp_t *cp = t2->cp;
321 int compno = pi->compno; /* component value */
322 int resno = pi->resno; /* resolution level value */
323 int precno = pi->precno; /* precinct value */
324 int layno = pi->layno; /* quality layer value */
326 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
327 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
329 unsigned char *hd = NULL;
332 opj_bio_t *bio = NULL; /* BIO component */
335 for (bandno = 0; bandno < res->numbands; bandno++) {
336 opj_tcd_band_t *band = &res->bands[bandno];
337 opj_tcd_precinct_t *prc = &band->precincts[precno];
339 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
341 tgt_reset(prc->incltree);
342 tgt_reset(prc->imsbtree);
343 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
344 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
352 if (tcp->csty & J2K_CP_CSTY_SOP) {
353 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
354 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
359 /** TODO : check the Nsop value */
363 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
364 This part deal with this caracteristic
365 step 1: Read packet header in the saved structure
366 step 2: Return to codestream for decoding
371 if (cp->ppm == 1) { /* PPM */
373 bio_init_dec(bio, hd, cp->ppm_len);
374 } else if (tcp->ppt == 1) { /* PPT */
376 bio_init_dec(bio, hd, tcp->ppt_len);
377 } else { /* Normal Case */
379 bio_init_dec(bio, hd, src+len-hd);
382 present = bio_read(bio, 1);
386 hd += bio_numbytes(bio);
391 if (tcp->csty & J2K_CP_CSTY_EPH) {
392 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
393 printf("Error : expected EPH marker\n");
400 // End of packet header position. Currently only represents the distance to start of packet
401 // Will be updated later by incrementing with packet start value
403 pack_info->end_ph_pos = (int)(c - src);
407 if (cp->ppm == 1) { /* PPM case */
408 cp->ppm_len += cp->ppm_data-hd;
412 if (tcp->ppt == 1) { /* PPT case */
413 tcp->ppt_len+=tcp->ppt_data-hd;
421 for (bandno = 0; bandno < res->numbands; bandno++) {
422 opj_tcd_band_t *band = &res->bands[bandno];
423 opj_tcd_precinct_t *prc = &band->precincts[precno];
425 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
427 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
428 int included, increment, n;
429 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
430 opj_tcd_seg_t *seg = NULL;
431 /* if cblk not yet included before --> inclusion tagtree */
432 if (!cblk->numsegs) {
433 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
436 included = bio_read(bio, 1);
438 /* if cblk not included */
440 cblk->numnewpasses = 0;
443 /* if cblk not yet included --> zero-bitplane tagtree */
444 if (!cblk->numsegs) {
446 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
450 cblk->numbps = band->numbps - numimsbs;
451 cblk->numlenbits = 3;
453 /* number of coding passes */
454 cblk->numnewpasses = t2_getnumpasses(bio);
455 increment = t2_getcommacode(bio);
456 /* length indicator increment */
457 cblk->numlenbits += increment;
458 if (!cblk->numsegs) {
459 seg = &cblk->segs[0];
460 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
462 seg = &cblk->segs[cblk->numsegs - 1];
463 if (seg->numpasses == seg->maxpasses) {
464 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
467 n = cblk->numnewpasses;
470 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
471 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
472 n -= seg->numnewpasses;
474 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
480 if (bio_inalign(bio)) {
485 hd += bio_numbytes(bio);
489 if (tcp->csty & J2K_CP_CSTY_EPH) {
490 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
491 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
498 // End of packet header position. Currently only represents the distance to start of packet
499 // Will be updated later by incrementing with packet start value
501 pack_info->end_ph_pos = (int)(hd - src);
506 cp->ppm_len+=cp->ppm_data-hd;
508 } else if (tcp->ppt == 1) {
509 tcp->ppt_len+=tcp->ppt_data-hd;
515 for (bandno = 0; bandno < res->numbands; bandno++) {
516 opj_tcd_band_t *band = &res->bands[bandno];
517 opj_tcd_precinct_t *prc = &band->precincts[precno];
519 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
521 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
522 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
523 opj_tcd_seg_t *seg = NULL;
524 if (!cblk->numnewpasses)
526 if (!cblk->numsegs) {
527 seg = &cblk->segs[0];
531 seg = &cblk->segs[cblk->numsegs - 1];
532 if (seg->numpasses == seg->maxpasses) {
539 if (c + seg->newlen > src + len) {
544 /* we need here a j2k handle to verify if making a check to
545 the validity of cblocks parameters is selected from user (-W) */
547 /* let's check that we are not exceeding */
548 if ((cblk->len + seg->newlen) > 8192) {
549 opj_event_msg(t2->cinfo, EVT_WARNING,
550 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
551 seg->newlen, cblkno, precno, bandno, resno, compno);
553 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
556 seg->newlen = 8192 - cblk->len;
557 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
561 #endif /* USE_JPWL */
563 memcpy(cblk->data + cblk->len, c, seg->newlen);
564 if (seg->numpasses == 0) {
565 seg->data = cblk->data + cblk->len;
568 cblk->len += seg->newlen;
569 seg->len += seg->newlen;
570 seg->numpasses += seg->numnewpasses;
571 cblk->numnewpasses -= seg->numnewpasses;
572 if (cblk->numnewpasses > 0) {
576 } while (cblk->numnewpasses > 0);
583 /* ----------------------------------------------------------------------- */
585 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){
586 unsigned char *c = dest;
590 opj_pi_iterator_t *pi = NULL;
592 opj_image_t *image = t2->image;
593 opj_cp_t *cp = t2->cp;
594 int pocno = cp->cinema == CINEMA4K_24? 2: 1;
595 int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
597 pi = pi_initialise_encode(image, cp, tileno, t2_mode);
599 /* TODO: throw an error */
603 if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
604 for(compno = 0; compno < maxcomp; compno++ ){
605 for(poc = 0; poc < pocno ; poc++){
608 pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode);
609 while (pi_next(&pi[poc])) {
610 if (pi[poc].layno < maxlayers) {
611 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
612 comp_len = comp_len + e;
620 if (e == -999) break;
621 if (cp->max_comp_size){
622 if (comp_len > cp->max_comp_size){
628 if (e == -999) break;
630 }else{ /* t2_mode == FINAL_PASS */
631 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode);
632 while (pi_next(&pi[pino])) {
633 if (pi[pino].layno < maxlayers) {
634 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
642 if(cstr_info->index_write) {
643 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
644 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
645 if (!cstr_info->packno) {
646 info_PK->start_pos = info_TL->end_header + 1;
648 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;
650 info_PK->end_pos = info_PK->start_pos + e - 1;
651 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
652 // to start of packet is incremented by value of start of packet
662 pi_destroy(pi, cp, tileno);
671 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) {
672 unsigned char *c = src;
673 opj_pi_iterator_t *pi;
675 int n = 0, curtp = 0;
678 opj_image_t *image = t2->image;
679 opj_cp_t *cp = t2->cp;
681 /* create a packet iterator */
682 pi = pi_create_decode(image, cp, tileno);
684 /* TODO: throw an error */
690 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
691 while (pi_next(&pi[pino])) {
692 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
693 opj_packet_info_t *pack_info;
695 pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
698 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
703 /* progression in resolution */
704 image->comps[pi[pino].compno].resno_decoded =
706 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
707 : image->comps[pi[pino].compno].resno_decoded;
712 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
713 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
714 if (!cstr_info->packno) {
715 info_PK->start_pos = info_TL->end_header + 1;
716 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ // New tile part
717 info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
718 tp_start_packno = cstr_info->packno;
720 info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
722 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;
724 info_PK->end_pos = info_PK->start_pos + e - 1;
725 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
726 // to start of packet is incremented by value of start of packet
731 if (e == -999) { /* ADD */
740 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in last tile-part
744 /* don't forget to release pi */
745 pi_destroy(pi, cp, tileno);
754 /* ----------------------------------------------------------------------- */
756 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
757 /* create the tcd structure */
758 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
767 void t2_destroy(opj_t2_t *t2) {