2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Hervé Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_includes.h"
33 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
36 /** @name Local static functions */
39 static void t2_putcommacode(opj_bio_t *bio, int n);
40 static int t2_getcommacode(opj_bio_t *bio);
42 Variable length code for signalling delta Zil (truncation point)
43 @param bio Bit Input/Output component
46 static void t2_putnumpasses(opj_bio_t *bio, int n);
47 static int t2_getnumpasses(opj_bio_t *bio);
49 Encode a packet of a tile to a destination buffer
50 @param tile Tile for which to write the packets
51 @param tcp Tile coding parameters
52 @param pi Packet identity
53 @param dest Destination buffer
54 @param len Length of the destination buffer
55 @param image_info Structure to create an index file
56 @param tileno Number of the tile encoded
59 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);
65 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
67 Decode a packet of a tile from a source buffer
69 @param src Source buffer
70 @param len Length of the source buffer
71 @param tile Tile for which to write the packets
72 @param tcp Tile coding parameters
73 @param pi Packet identity
76 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);
82 /* ----------------------------------------------------------------------- */
84 /* #define RESTART 0x04 */
86 static void t2_putcommacode(opj_bio_t *bio, int n) {
93 static int t2_getcommacode(opj_bio_t *bio) {
95 for (n = 0; bio_read(bio, 1); n++) {
101 static void t2_putnumpasses(opj_bio_t *bio, int n) {
103 bio_write(bio, 0, 1);
105 bio_write(bio, 2, 2);
107 bio_write(bio, 0xc | (n - 3), 4);
108 } else if (n <= 36) {
109 bio_write(bio, 0x1e0 | (n - 6), 9);
110 } else if (n <= 164) {
111 bio_write(bio, 0xff80 | (n - 37), 16);
115 static int t2_getnumpasses(opj_bio_t *bio) {
117 if (!bio_read(bio, 1))
119 if (!bio_read(bio, 1))
121 if ((n = bio_read(bio, 2)) != 3)
123 if ((n = bio_read(bio, 5)) != 31)
125 return (37 + bio_read(bio, 7));
128 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) {
130 unsigned char *sop = 0, *eph = 0;
131 unsigned char *c = dest;
133 int compno = pi->compno; /* component value */
134 int resno = pi->resno; /* resolution level value */
135 int precno = pi->precno; /* precinct value */
136 int layno = pi->layno; /* quality layer value */
138 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
139 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
141 opj_bio_t *bio = NULL; /* BIO component */
144 if (tcp->csty & J2K_CP_CSTY_SOP) {
145 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
150 sop[4] = (image_info->num % 65536) / 256;
151 sop[5] = (image_info->num % 65536) % 256;
159 for (bandno = 0; bandno < res->numbands; bandno++) {
160 opj_tcd_band_t *band = &res->bands[bandno];
161 opj_tcd_precinct_t *prc = &band->precincts[precno];
162 tgt_reset(prc->incltree);
163 tgt_reset(prc->imsbtree);
164 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
165 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
167 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
173 bio_init_enc(bio, c, len);
174 bio_write(bio, 1, 1); /* Empty header bit */
176 /* Writing Packet header */
177 for (bandno = 0; bandno < res->numbands; bandno++) {
178 opj_tcd_band_t *band = &res->bands[bandno];
179 opj_tcd_precinct_t *prc = &band->precincts[precno];
180 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
181 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
182 opj_tcd_layer_t *layer = &cblk->layers[layno];
183 if (!cblk->numpasses && layer->numpasses) {
184 tgt_setvalue(prc->incltree, cblkno, layno);
187 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
188 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
189 opj_tcd_layer_t *layer = &cblk->layers[layno];
193 /* cblk inclusion bits */
194 if (!cblk->numpasses) {
195 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
197 bio_write(bio, layer->numpasses != 0, 1);
199 /* if cblk not included, go to the next cblk */
200 if (!layer->numpasses) {
203 /* if first instance of cblk --> zero bit-planes information */
204 if (!cblk->numpasses) {
205 cblk->numlenbits = 3;
206 tgt_encode(bio, prc->imsbtree, cblkno, 999);
208 /* number of coding passes included */
209 t2_putnumpasses(bio, layer->numpasses);
211 /* computation of the increase of the length indicator and insertion in the header */
212 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
213 opj_tcd_pass_t *pass = &cblk->passes[passno];
216 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
217 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
222 t2_putcommacode(bio, increment);
224 /* computation of the new Length indicator */
225 cblk->numlenbits += increment;
227 /* insertion of the codeword segment length */
228 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
229 opj_tcd_pass_t *pass = &cblk->passes[passno];
232 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
233 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
241 if (bio_flush(bio)) {
242 return -999; /* modified to eliminate longjmp !! */
245 c += bio_numbytes(bio);
250 if (tcp->csty & J2K_CP_CSTY_EPH) {
251 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
260 /* Writing the packet body */
262 for (bandno = 0; bandno < res->numbands; bandno++) {
263 opj_tcd_band_t *band = &res->bands[bandno];
264 opj_tcd_precinct_t *prc = &band->precincts[precno];
265 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
266 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
267 opj_tcd_layer_t *layer = &cblk->layers[layno];
268 if (!layer->numpasses) {
271 if (c + layer->len > dest + len) {
275 memcpy(c, layer->data, layer->len);
276 cblk->numpasses += layer->numpasses;
278 /* ADD for index Cfr. Marcela --> delta disto by packet */
279 if(image_info && image_info->index_write && image_info->index_on) {
280 opj_tile_info_t *info_TL = &image_info->tile[tileno];
281 opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
282 info_PK->disto += layer->disto;
283 if (image_info->D_max < info_PK->disto) {
284 image_info->D_max = info_PK->disto;
294 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
297 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
300 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
304 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
307 seg->maxpasses = 109;
311 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) {
313 unsigned char *c = src;
315 opj_cp_t *cp = t2->cp;
317 int compno = pi->compno; /* component value */
318 int resno = pi->resno; /* resolution level value */
319 int precno = pi->precno; /* precinct value */
320 int layno = pi->layno; /* quality layer value */
322 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
323 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
325 unsigned char *hd = NULL;
328 opj_bio_t *bio = NULL; /* BIO component */
331 for (bandno = 0; bandno < res->numbands; bandno++) {
332 opj_tcd_band_t *band = &res->bands[bandno];
333 opj_tcd_precinct_t *prc = &band->precincts[precno];
335 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
337 tgt_reset(prc->incltree);
338 tgt_reset(prc->imsbtree);
339 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
340 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
348 if (tcp->csty & J2K_CP_CSTY_SOP) {
349 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
350 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
355 /** TODO : check the Nsop value */
359 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
360 This part deal with this caracteristic
361 step 1: Read packet header in the saved structure
362 step 2: Return to codestream for decoding
367 if (cp->ppm == 1) { /* PPM */
369 bio_init_dec(bio, hd, cp->ppm_len);
370 } else if (tcp->ppt == 1) { /* PPT */
372 bio_init_dec(bio, hd, tcp->ppt_len);
373 } else { /* Normal Case */
375 bio_init_dec(bio, hd, src+len-hd);
378 present = bio_read(bio, 1);
382 hd += bio_numbytes(bio);
387 if (tcp->csty & J2K_CP_CSTY_EPH) {
388 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
389 printf("Error : expected EPH marker\n");
395 if (cp->ppm == 1) { /* PPM case */
396 cp->ppm_len += cp->ppm_data-hd;
400 if (tcp->ppt == 1) { /* PPT case */
401 tcp->ppt_len+=tcp->ppt_data-hd;
409 for (bandno = 0; bandno < res->numbands; bandno++) {
410 opj_tcd_band_t *band = &res->bands[bandno];
411 opj_tcd_precinct_t *prc = &band->precincts[precno];
413 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
415 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
416 int included, increment, n;
417 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
418 opj_tcd_seg_t *seg = NULL;
419 /* if cblk not yet included before --> inclusion tagtree */
420 if (!cblk->numsegs) {
421 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
424 included = bio_read(bio, 1);
426 /* if cblk not included */
428 cblk->numnewpasses = 0;
431 /* if cblk not yet included --> zero-bitplane tagtree */
432 if (!cblk->numsegs) {
434 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
438 cblk->numbps = band->numbps - numimsbs;
439 cblk->numlenbits = 3;
441 /* number of coding passes */
442 cblk->numnewpasses = t2_getnumpasses(bio);
443 increment = t2_getcommacode(bio);
444 /* length indicator increment */
445 cblk->numlenbits += increment;
446 if (!cblk->numsegs) {
447 seg = &cblk->segs[0];
448 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
450 seg = &cblk->segs[cblk->numsegs - 1];
451 if (seg->numpasses == seg->maxpasses) {
452 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
455 n = cblk->numnewpasses;
458 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
459 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
460 n -= seg->numnewpasses;
462 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
468 if (bio_inalign(bio)) {
473 hd += bio_numbytes(bio);
477 if (tcp->csty & J2K_CP_CSTY_EPH) {
478 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
479 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
486 cp->ppm_len+=cp->ppm_data-hd;
488 } else if (tcp->ppt == 1) {
489 tcp->ppt_len+=tcp->ppt_data-hd;
495 for (bandno = 0; bandno < res->numbands; bandno++) {
496 opj_tcd_band_t *band = &res->bands[bandno];
497 opj_tcd_precinct_t *prc = &band->precincts[precno];
499 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
501 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
502 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
503 opj_tcd_seg_t *seg = NULL;
504 if (!cblk->numnewpasses)
506 if (!cblk->numsegs) {
507 seg = &cblk->segs[0];
511 seg = &cblk->segs[cblk->numsegs - 1];
512 if (seg->numpasses == seg->maxpasses) {
519 if (c + seg->newlen > src + len) {
523 memcpy(cblk->data + cblk->len, c, seg->newlen);
524 if (seg->numpasses == 0) {
525 seg->data = cblk->data + cblk->len;
528 cblk->len += seg->newlen;
529 seg->len += seg->newlen;
530 seg->numpasses += seg->numnewpasses;
531 cblk->numnewpasses -= seg->numnewpasses;
532 if (cblk->numnewpasses > 0) {
536 } while (cblk->numnewpasses > 0);
543 /* ----------------------------------------------------------------------- */
545 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) {
546 unsigned char *c = dest;
548 opj_pi_iterator_t *pi = NULL;
551 opj_image_t *image = t2->image;
552 opj_cp_t *cp = t2->cp;
554 /* create a packet iterator */
555 pi = pi_create(image, cp, tileno);
557 /* TODO: throw an error */
565 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
566 while (pi_next(&pi[pino])) {
567 if (pi[pino].layno < maxlayers) {
568 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, image_info, tileno);
576 if(image_info && image_info->index_on) {
577 if(image_info->index_write) {
578 opj_tile_info_t *info_TL = &image_info->tile[tileno];
579 opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
580 if (!image_info->num) {
581 info_PK->start_pos = info_TL->end_header + 1;
583 info_PK->start_pos = info_TL->packet[image_info->num - 1].end_pos + 1;
585 info_PK->end_pos = info_PK->start_pos + e - 1;
595 /* don't forget to release pi */
596 pi_destroy(pi, cp, tileno);
605 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
606 unsigned char *c = src;
607 opj_pi_iterator_t *pi;
611 opj_image_t *image = t2->image;
612 opj_cp_t *cp = t2->cp;
614 /* create a packet iterator */
615 pi = pi_create(image, cp, tileno);
617 /* TODO: throw an error */
621 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
622 while (pi_next(&pi[pino])) {
623 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
624 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
629 /* progression in resolution */
630 image->comps[pi[pino].compno].resno_decoded =
632 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
633 : image->comps[pi[pino].compno].resno_decoded;
636 if (e == -999) { /* ADD */
644 /* don't forget to release pi */
645 pi_destroy(pi, cp, tileno);
654 /* ----------------------------------------------------------------------- */
656 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
657 /* create the tcd structure */
658 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
667 void t2_destroy(opj_t2_t *t2) {