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 image_info Structure to create an index file
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_image_info_t *image_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, opj_tcp_t *tcp, opj_pi_iterator_t *pi);
83 /* ----------------------------------------------------------------------- */
85 /* #define RESTART 0x04 */
87 static void t2_putcommacode(opj_bio_t *bio, int n) {
94 static int t2_getcommacode(opj_bio_t *bio) {
96 for (n = 0; bio_read(bio, 1); n++) {
102 static void t2_putnumpasses(opj_bio_t *bio, int n) {
104 bio_write(bio, 0, 1);
106 bio_write(bio, 2, 2);
108 bio_write(bio, 0xc | (n - 3), 4);
109 } else if (n <= 36) {
110 bio_write(bio, 0x1e0 | (n - 6), 9);
111 } else if (n <= 164) {
112 bio_write(bio, 0xff80 | (n - 37), 16);
116 static int t2_getnumpasses(opj_bio_t *bio) {
118 if (!bio_read(bio, 1))
120 if (!bio_read(bio, 1))
122 if ((n = bio_read(bio, 2)) != 3)
124 if ((n = bio_read(bio, 5)) != 31)
126 return (37 + bio_read(bio, 7));
129 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_image_info_t * image_info, int tileno) {
131 unsigned char *sop = 0, *eph = 0;
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) {
146 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
151 sop[4] = (image_info->num % 65536) / 256;
152 sop[5] = (image_info->num % 65536) % 256;
160 for (bandno = 0; bandno < res->numbands; bandno++) {
161 opj_tcd_band_t *band = &res->bands[bandno];
162 opj_tcd_precinct_t *prc = &band->precincts[precno];
163 tgt_reset(prc->incltree);
164 tgt_reset(prc->imsbtree);
165 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
166 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
168 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
174 bio_init_enc(bio, c, length);
175 bio_write(bio, 1, 1); /* Empty header bit */
177 /* Writing Packet header */
178 for (bandno = 0; bandno < res->numbands; bandno++) {
179 opj_tcd_band_t *band = &res->bands[bandno];
180 opj_tcd_precinct_t *prc = &band->precincts[precno];
181 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
182 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
183 opj_tcd_layer_t *layer = &cblk->layers[layno];
184 if (!cblk->numpasses && layer->numpasses) {
185 tgt_setvalue(prc->incltree, cblkno, layno);
188 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
189 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
190 opj_tcd_layer_t *layer = &cblk->layers[layno];
194 /* cblk inclusion bits */
195 if (!cblk->numpasses) {
196 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
198 bio_write(bio, layer->numpasses != 0, 1);
200 /* if cblk not included, go to the next cblk */
201 if (!layer->numpasses) {
204 /* if first instance of cblk --> zero bit-planes information */
205 if (!cblk->numpasses) {
206 cblk->numlenbits = 3;
207 tgt_encode(bio, prc->imsbtree, cblkno, 999);
209 /* number of coding passes included */
210 t2_putnumpasses(bio, layer->numpasses);
212 /* computation of the increase of the length indicator and insertion in the header */
213 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
214 opj_tcd_pass_t *pass = &cblk->passes[passno];
217 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
218 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
223 t2_putcommacode(bio, increment);
225 /* computation of the new Length indicator */
226 cblk->numlenbits += increment;
228 /* insertion of the codeword segment length */
229 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
230 opj_tcd_pass_t *pass = &cblk->passes[passno];
233 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
234 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
242 if (bio_flush(bio)) {
243 return -999; /* modified to eliminate longjmp !! */
246 c += bio_numbytes(bio);
251 if (tcp->csty & J2K_CP_CSTY_EPH) {
252 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
261 /* Writing the packet body */
263 for (bandno = 0; bandno < res->numbands; bandno++) {
264 opj_tcd_band_t *band = &res->bands[bandno];
265 opj_tcd_precinct_t *prc = &band->precincts[precno];
266 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
267 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
268 opj_tcd_layer_t *layer = &cblk->layers[layno];
269 if (!layer->numpasses) {
272 if (c + layer->len > dest + length) {
276 memcpy(c, layer->data, layer->len);
277 cblk->numpasses += layer->numpasses;
279 /* ADD for index Cfr. Marcela --> delta disto by packet */
280 if(image_info && image_info->index_write && image_info->index_on) {
281 opj_tile_info_t *info_TL = &image_info->tile[tileno];
282 opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
283 info_PK->disto += layer->disto;
284 if (image_info->D_max < info_PK->disto) {
285 image_info->D_max = info_PK->disto;
295 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
298 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
301 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
305 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
308 seg->maxpasses = 109;
312 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) {
314 unsigned char *c = src;
316 opj_cp_t *cp = t2->cp;
318 int compno = pi->compno; /* component value */
319 int resno = pi->resno; /* resolution level value */
320 int precno = pi->precno; /* precinct value */
321 int layno = pi->layno; /* quality layer value */
323 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
324 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
326 unsigned char *hd = NULL;
329 opj_bio_t *bio = NULL; /* BIO component */
332 for (bandno = 0; bandno < res->numbands; bandno++) {
333 opj_tcd_band_t *band = &res->bands[bandno];
334 opj_tcd_precinct_t *prc = &band->precincts[precno];
336 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
338 tgt_reset(prc->incltree);
339 tgt_reset(prc->imsbtree);
340 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
341 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
349 if (tcp->csty & J2K_CP_CSTY_SOP) {
350 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
351 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
356 /** TODO : check the Nsop value */
360 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
361 This part deal with this caracteristic
362 step 1: Read packet header in the saved structure
363 step 2: Return to codestream for decoding
368 if (cp->ppm == 1) { /* PPM */
370 bio_init_dec(bio, hd, cp->ppm_len);
371 } else if (tcp->ppt == 1) { /* PPT */
373 bio_init_dec(bio, hd, tcp->ppt_len);
374 } else { /* Normal Case */
376 bio_init_dec(bio, hd, src+len-hd);
379 present = bio_read(bio, 1);
383 hd += bio_numbytes(bio);
388 if (tcp->csty & J2K_CP_CSTY_EPH) {
389 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
390 printf("Error : expected EPH marker\n");
396 if (cp->ppm == 1) { /* PPM case */
397 cp->ppm_len += cp->ppm_data-hd;
401 if (tcp->ppt == 1) { /* PPT case */
402 tcp->ppt_len+=tcp->ppt_data-hd;
410 for (bandno = 0; bandno < res->numbands; bandno++) {
411 opj_tcd_band_t *band = &res->bands[bandno];
412 opj_tcd_precinct_t *prc = &band->precincts[precno];
414 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
416 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
417 int included, increment, n;
418 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
419 opj_tcd_seg_t *seg = NULL;
420 /* if cblk not yet included before --> inclusion tagtree */
421 if (!cblk->numsegs) {
422 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
425 included = bio_read(bio, 1);
427 /* if cblk not included */
429 cblk->numnewpasses = 0;
432 /* if cblk not yet included --> zero-bitplane tagtree */
433 if (!cblk->numsegs) {
435 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
439 cblk->numbps = band->numbps - numimsbs;
440 cblk->numlenbits = 3;
442 /* number of coding passes */
443 cblk->numnewpasses = t2_getnumpasses(bio);
444 increment = t2_getcommacode(bio);
445 /* length indicator increment */
446 cblk->numlenbits += increment;
447 if (!cblk->numsegs) {
448 seg = &cblk->segs[0];
449 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
451 seg = &cblk->segs[cblk->numsegs - 1];
452 if (seg->numpasses == seg->maxpasses) {
453 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
456 n = cblk->numnewpasses;
459 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
460 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
461 n -= seg->numnewpasses;
463 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
469 if (bio_inalign(bio)) {
474 hd += bio_numbytes(bio);
478 if (tcp->csty & J2K_CP_CSTY_EPH) {
479 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
480 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
487 cp->ppm_len+=cp->ppm_data-hd;
489 } else if (tcp->ppt == 1) {
490 tcp->ppt_len+=tcp->ppt_data-hd;
496 for (bandno = 0; bandno < res->numbands; bandno++) {
497 opj_tcd_band_t *band = &res->bands[bandno];
498 opj_tcd_precinct_t *prc = &band->precincts[precno];
500 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
502 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
503 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
504 opj_tcd_seg_t *seg = NULL;
505 if (!cblk->numnewpasses)
507 if (!cblk->numsegs) {
508 seg = &cblk->segs[0];
512 seg = &cblk->segs[cblk->numsegs - 1];
513 if (seg->numpasses == seg->maxpasses) {
520 if (c + seg->newlen > src + len) {
525 /* we need here a j2k handle to verify if making a check to
526 the validity of cblocks parameters is selected from user (-W) */
528 /* let's check that we are not exceeding */
529 if ((cblk->len + seg->newlen) > 8192) {
530 opj_event_msg(t2->cinfo, EVT_WARNING,
531 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
532 seg->newlen, cblkno, precno, bandno, resno, compno);
534 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
537 seg->newlen = 8192 - cblk->len;
538 opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen);
542 #endif /* USE_JPWL */
544 memcpy(cblk->data + cblk->len, c, seg->newlen);
545 if (seg->numpasses == 0) {
546 seg->data = cblk->data + cblk->len;
549 cblk->len += seg->newlen;
550 seg->len += seg->newlen;
551 seg->numpasses += seg->numnewpasses;
552 cblk->numnewpasses -= seg->numnewpasses;
553 if (cblk->numnewpasses > 0) {
557 } while (cblk->numnewpasses > 0);
564 /* ----------------------------------------------------------------------- */
566 int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode){
567 unsigned char *c = dest;
571 opj_pi_iterator_t *pi = NULL;
573 opj_image_t *image = t2->image;
574 opj_cp_t *cp = t2->cp;
575 int pocno = cp->cinema == CINEMA4K_24? 2: 1;
576 int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
578 pi = pi_initialise_encode(image, cp, tileno, t2_mode);
580 /* TODO: throw an error */
584 if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
585 for(compno = 0; compno < maxcomp; compno++ ){
586 for(poc = 0; poc < pocno ; poc++){
589 pi_create_encode(pi, cp,tileno,poc,tpnum,tppos);
590 while (pi_next(&pi[poc])) {
591 if (pi[poc].layno < maxlayers) {
592 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, image_info, tileno);
593 comp_len = comp_len + e;
601 if (e == -999) break;
602 if (cp->max_comp_size){
603 if (comp_len > cp->max_comp_size){
609 if (e == -999) break;
611 }else{ /* t2_mode == FINAL_PASS */
612 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos);
613 while (pi_next(&pi[pino])) {
614 if (pi[pino].layno < maxlayers) {
615 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, image_info, tileno);
622 if(image_info && image_info->index_on) {
623 if(image_info->index_write) {
624 opj_tile_info_t *info_TL = &image_info->tile[tileno];
625 opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
626 if (!image_info->num) {
627 info_PK->start_pos = info_TL->end_header + 1;
629 info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[image_info->num - 1].end_pos + 1;
631 info_PK->end_pos = info_PK->start_pos + e - 1;
641 pi_destroy(pi, cp, tileno);
650 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
651 unsigned char *c = src;
652 opj_pi_iterator_t *pi;
656 opj_image_t *image = t2->image;
657 opj_cp_t *cp = t2->cp;
659 /* create a packet iterator */
660 pi = pi_create_decode(image, cp, tileno);
662 /* TODO: throw an error */
666 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
667 while (pi_next(&pi[pino])) {
668 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
669 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
674 /* progression in resolution */
675 image->comps[pi[pino].compno].resno_decoded =
677 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
678 : image->comps[pi[pino].compno].resno_decoded;
681 if (e == -999) { /* ADD */
689 /* don't forget to release pi */
690 pi_destroy(pi, cp, tileno);
699 /* ----------------------------------------------------------------------- */
701 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
702 /* create the tcd structure */
703 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
712 void t2_destroy(opj_t2_t *t2) {