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_cblk_dec_t* cblk, int index, 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] = (tile->packno % 65536) / 256;
151 c[5] = (tile->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_enc_t* cblk = &prc->cblks.enc[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_enc_t* cblk = &prc->cblks.enc[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_enc_t* cblk = &prc->cblks.enc[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_enc_t* cblk = &prc->cblks.enc[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_cblk_dec_t* cblk, int index, int cblksty, int first) {
299 cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
300 seg = &cblk->segs[index];
305 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
308 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
312 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
315 seg->maxpasses = 109;
319 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile,
320 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
322 unsigned char *c = src;
324 opj_cp_t *cp = t2->cp;
326 int compno = pi->compno; /* component value */
327 int resno = pi->resno; /* resolution level value */
328 int precno = pi->precno; /* precinct value */
329 int layno = pi->layno; /* quality layer value */
331 opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno];
333 unsigned char *hd = NULL;
336 opj_bio_t *bio = NULL; /* BIO component */
339 for (bandno = 0; bandno < res->numbands; bandno++) {
340 opj_tcd_band_t *band = &res->bands[bandno];
341 opj_tcd_precinct_t *prc = &band->precincts[precno];
343 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
345 tgt_reset(prc->incltree);
346 tgt_reset(prc->imsbtree);
347 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
348 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
356 if (tcp->csty & J2K_CP_CSTY_SOP) {
357 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
358 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
363 /** TODO : check the Nsop value */
367 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
368 This part deal with this caracteristic
369 step 1: Read packet header in the saved structure
370 step 2: Return to codestream for decoding
375 if (cp->ppm == 1) { /* PPM */
377 bio_init_dec(bio, hd, cp->ppm_len);
378 } else if (tcp->ppt == 1) { /* PPT */
380 bio_init_dec(bio, hd, tcp->ppt_len);
381 } else { /* Normal Case */
383 bio_init_dec(bio, hd, src+len-hd);
386 present = bio_read(bio, 1);
390 hd += bio_numbytes(bio);
395 if (tcp->csty & J2K_CP_CSTY_EPH) {
396 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
397 printf("Error : expected EPH marker\n");
404 // End of packet header position. Currently only represents the distance to start of packet
405 // Will be updated later by incrementing with packet start value
407 pack_info->end_ph_pos = (int)(c - src);
411 if (cp->ppm == 1) { /* PPM case */
412 cp->ppm_len += cp->ppm_data-hd;
416 if (tcp->ppt == 1) { /* PPT case */
417 tcp->ppt_len+=tcp->ppt_data-hd;
425 for (bandno = 0; bandno < res->numbands; bandno++) {
426 opj_tcd_band_t *band = &res->bands[bandno];
427 opj_tcd_precinct_t *prc = &band->precincts[precno];
429 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
431 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
432 int included, increment, n, segno;
433 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
434 /* if cblk not yet included before --> inclusion tagtree */
435 if (!cblk->numsegs) {
436 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
439 included = bio_read(bio, 1);
441 /* if cblk not included */
443 cblk->numnewpasses = 0;
446 /* if cblk not yet included --> zero-bitplane tagtree */
447 if (!cblk->numsegs) {
449 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
453 cblk->numbps = band->numbps - numimsbs;
454 cblk->numlenbits = 3;
456 /* number of coding passes */
457 cblk->numnewpasses = t2_getnumpasses(bio);
458 increment = t2_getcommacode(bio);
459 /* length indicator increment */
460 cblk->numlenbits += increment;
462 if (!cblk->numsegs) {
463 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1);
465 segno = cblk->numsegs - 1;
466 if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
468 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
471 n = cblk->numnewpasses;
474 cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
475 cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
476 n -= cblk->segs[segno].numnewpasses;
479 t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0);
485 if (bio_inalign(bio)) {
490 hd += bio_numbytes(bio);
494 if (tcp->csty & J2K_CP_CSTY_EPH) {
495 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
496 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
503 // End of packet header position. Currently only represents the distance to start of packet
504 // Will be updated later by incrementing with packet start value
506 pack_info->end_ph_pos = (int)(hd - src);
511 cp->ppm_len+=cp->ppm_data-hd;
513 } else if (tcp->ppt == 1) {
514 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 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
528 opj_tcd_seg_t *seg = NULL;
529 if (!cblk->numnewpasses)
531 if (!cblk->numsegs) {
532 seg = &cblk->segs[0];
536 seg = &cblk->segs[cblk->numsegs - 1];
537 if (seg->numpasses == seg->maxpasses) {
544 if (c + seg->newlen > src + len) {
549 /* we need here a j2k handle to verify if making a check to
550 the validity of cblocks parameters is selected from user (-W) */
552 /* let's check that we are not exceeding */
553 if ((cblk->len + seg->newlen) > 8192) {
554 opj_event_msg(t2->cinfo, EVT_WARNING,
555 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
556 seg->newlen, cblkno, precno, bandno, resno, compno);
558 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
561 seg->newlen = 8192 - cblk->len;
562 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
566 #endif /* USE_JPWL */
568 cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char*));
569 memcpy(cblk->data + cblk->len, c, seg->newlen);
570 if (seg->numpasses == 0) {
571 seg->data = &cblk->data;
572 seg->dataindex = cblk->len;
575 cblk->len += seg->newlen;
576 seg->len += seg->newlen;
577 seg->numpasses += seg->numnewpasses;
578 cblk->numnewpasses -= seg->numnewpasses;
579 if (cblk->numnewpasses > 0) {
583 } while (cblk->numnewpasses > 0);
590 /* ----------------------------------------------------------------------- */
592 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){
593 unsigned char *c = dest;
596 opj_pi_iterator_t *pi = NULL;
598 opj_image_t *image = t2->image;
599 opj_cp_t *cp = t2->cp;
600 opj_tcp_t *tcp = &cp->tcps[tileno];
601 int pocno = cp->cinema == CINEMA4K_24? 2: 1;
602 int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
604 pi = pi_initialise_encode(image, cp, tileno, t2_mode);
606 /* TODO: throw an error */
610 if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
611 for(compno = 0; compno < maxcomp; compno++ ){
612 for(poc = 0; poc < pocno ; poc++){
615 if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
616 opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
617 pi_destroy(pi, cp, tileno);
620 while (pi_next(&pi[poc])) {
621 if (pi[poc].layno < maxlayers) {
622 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
623 comp_len = comp_len + e;
631 if (e == -999) break;
632 if (cp->max_comp_size){
633 if (comp_len > cp->max_comp_size){
639 if (e == -999) break;
641 }else{ /* t2_mode == FINAL_PASS */
642 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
643 while (pi_next(&pi[pino])) {
644 if (pi[pino].layno < maxlayers) {
645 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
653 if(cstr_info->index_write) {
654 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
655 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
656 if (!cstr_info->packno) {
657 info_PK->start_pos = info_TL->end_header + 1;
659 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;
661 info_PK->end_pos = info_PK->start_pos + e - 1;
662 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
663 // to start of packet is incremented by value of start of packet
674 pi_destroy(pi, cp, tileno);
683 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) {
684 unsigned char *c = src;
685 opj_pi_iterator_t *pi;
687 int n = 0, curtp = 0;
690 opj_image_t *image = t2->image;
691 opj_cp_t *cp = t2->cp;
693 /* create a packet iterator */
694 pi = pi_create_decode(image, cp, tileno);
696 /* TODO: throw an error */
702 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
703 while (pi_next(&pi[pino])) {
704 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
705 opj_packet_info_t *pack_info;
707 pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
710 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
715 /* progression in resolution */
716 image->comps[pi[pino].compno].resno_decoded =
718 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
719 : image->comps[pi[pino].compno].resno_decoded;
724 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
725 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
726 if (!cstr_info->packno) {
727 info_PK->start_pos = info_TL->end_header + 1;
728 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ // New tile part
729 info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part
730 tp_start_packno = cstr_info->packno;
732 info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
734 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;
736 info_PK->end_pos = info_PK->start_pos + e - 1;
737 info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance
738 // to start of packet is incremented by value of start of packet
743 if (e == -999) { /* ADD */
752 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in last tile-part
756 /* don't forget to release pi */
757 pi_destroy(pi, cp, tileno);
766 /* ----------------------------------------------------------------------- */
768 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
769 /* create the tcd structure */
770 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
779 void t2_destroy(opj_t2_t *t2) {