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, Herve 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 volume_info Structure to create an index file
56 @param tileno Number of the tile encoded
57 @param cp Coding parameters
58 @return Number of bytes encoded from the packet
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_volume_info_t *volume_info, int tileno, opj_cp_t *cp);
62 Initialize the segment decoder
63 @param seg Segment instance
64 @param cblksty Codeblock style
65 @param first Is first segment
67 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
69 Decode a packet of a tile from a source buffer
71 @param src Source buffer
72 @param len Length of the source buffer
73 @param tile Tile for which to write the packets
74 @param tcp Tile coding parameters
75 @param pi Packet identity
76 @return Number of bytes decoded from the packet
78 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);
84 /* ----------------------------------------------------------------------- */
86 /* #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 len, opj_volume_info_t * volume_info, int tileno, opj_cp_t *cp) {
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 & J3D_CP_CSTY_SOP)) {
146 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
151 sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ;
152 sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 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->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; 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, len);
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->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; 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);
189 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
190 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
191 opj_tcd_layer_t *layer = &cblk->layers[layno];
195 /* cblk inclusion bits */
196 if (!cblk->numpasses) {
197 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
199 bio_write(bio, layer->numpasses != 0, 1);
201 /* if cblk not included, go to the next cblk */
202 if (!layer->numpasses) {
205 /* if first instance of cblk --> zero bit-planes information */
206 if (!cblk->numpasses) {
207 cblk->numlenbits = 3;
208 tgt_encode(bio, prc->imsbtree, cblkno, 999);
210 /* number of coding passes included */
211 t2_putnumpasses(bio, layer->numpasses);
213 /* computation of the increase of the length indicator and insertion in the header */
214 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
215 opj_tcd_pass_t *pass = &cblk->passes[passno];
218 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
219 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
224 t2_putcommacode(bio, increment);
226 /* computation of the new Length indicator */
227 cblk->numlenbits += increment;
229 /* insertion of the codeword segment length */
230 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
231 opj_tcd_pass_t *pass = &cblk->passes[passno];
234 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
235 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
245 if (bio_flush(bio)) {
246 return -999; /* modified to eliminate longjmp !! */
249 c += bio_numbytes(bio);
254 if (tcp->csty & J3D_CP_CSTY_EPH) {
255 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
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->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; 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 + len) {
279 memcpy(c, layer->data, layer->len);
280 cblk->numpasses += layer->numpasses;
282 /* ADD for index Cfr. Marcela --> delta disto by packet */
283 if(volume_info && volume_info->index_write && volume_info->index_on) {
284 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
285 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
286 info_PK->disto += layer->disto;
287 if (volume_info->D_max < info_PK->disto) {
288 volume_info->D_max = info_PK->disto;
298 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
301 if (cblksty & J3D_CCP_CBLKSTY_TERMALL) {
304 else if (cblksty & J3D_CCP_CBLKSTY_LAZY) {
308 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
311 seg->maxpasses = 109;
315 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) {
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)||(band->z1-band->z0 == 0)) continue;
341 tgt_reset(prc->incltree);
342 tgt_reset(prc->imsbtree);
343 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
344 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
352 if (tcp->csty & J3D_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 & J3D_CP_CSTY_EPH) {
392 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
393 printf("Error : expected EPH marker\n");
399 if (cp->ppm == 1) { /* PPM case */
400 cp->ppm_len += cp->ppm_data-hd;
404 if (tcp->ppt == 1) { /* PPT case */
405 tcp->ppt_len+=tcp->ppt_data-hd;
413 for (bandno = 0; bandno < res->numbands; bandno++) {
414 opj_tcd_band_t *band = &res->bands[bandno];
415 opj_tcd_precinct_t *prc = &band->precincts[precno];
417 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
419 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
420 int included, increment, n;
421 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
422 opj_tcd_seg_t *seg = NULL;
423 /* if cblk not yet included before --> inclusion tagtree */
424 if (!cblk->numsegs) {
425 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
428 included = bio_read(bio, 1);
430 /* if cblk not included */
432 cblk->numnewpasses = 0;
435 /* if cblk not yet included --> zero-bitplane tagtree */
436 if (!cblk->numsegs) {
438 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++);
440 cblk->numbps = band->numbps - numimsbs;
441 cblk->numlenbits = 3;
443 /* number of coding passes */
444 cblk->numnewpasses = t2_getnumpasses(bio);
445 increment = t2_getcommacode(bio);
446 /* length indicator increment */
447 cblk->numlenbits += increment;
448 if (!cblk->numsegs) {
449 seg = &cblk->segs[0];
450 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
452 seg = &cblk->segs[cblk->numsegs - 1];
453 if (seg->numpasses == seg->maxpasses) {
454 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
457 n = cblk->numnewpasses;
460 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
461 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
462 n -= seg->numnewpasses;
464 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
470 if (bio_inalign(bio)) {
475 hd += bio_numbytes(bio);
479 if (tcp->csty & J3D_CP_CSTY_EPH) {
480 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
481 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
489 cp->ppm_len+=cp->ppm_data-hd;
491 } else if (tcp->ppt == 1) {
492 tcp->ppt_len+=tcp->ppt_data-hd;
498 for (bandno = 0; bandno < res->numbands; bandno++) {
499 opj_tcd_band_t *band = &res->bands[bandno];
500 opj_tcd_precinct_t *prc = &band->precincts[precno];
502 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
504 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
505 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
506 opj_tcd_seg_t *seg = NULL;
507 if (!cblk->numnewpasses)
509 if (!cblk->numsegs) {
510 seg = &cblk->segs[0];
514 seg = &cblk->segs[cblk->numsegs - 1];
515 if (seg->numpasses == seg->maxpasses) {
522 if (c + seg->newlen > src + len) {
526 memcpy(cblk->data + cblk->len, c, seg->newlen);
527 if (seg->numpasses == 0) {
528 seg->data = cblk->data + cblk->len;
531 cblk->len += seg->newlen;
532 seg->len += seg->newlen;
533 seg->numpasses += seg->numnewpasses;
534 cblk->numnewpasses -= seg->numnewpasses;
535 if (cblk->numnewpasses > 0) {
539 } while (cblk->numnewpasses > 0);
546 /* ----------------------------------------------------------------------- */
548 int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info) {
549 unsigned char *c = dest;
551 opj_pi_iterator_t *pi = NULL;
554 opj_volume_t *volume = t2->volume;
555 opj_cp_t *cp = t2->cp;
557 /* create a packet iterator */
558 pi = pi_create(volume, cp, tileno);
560 fprintf(stdout,"[ERROR] Failed to create a pi structure\n");
565 volume_info->num = 0;
568 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
569 while (pi_next(&pi[pino])) {
570 if (pi[pino].layno < maxlayers) {
571 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, volume_info, tileno, cp);
572 /*opj_event_msg(t2->cinfo, EVT_INFO, " t2_encode_packet: %d bytes coded\n",e);*/
580 if(volume_info && volume_info->index_on) {
581 if(volume_info->index_write) {
582 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
583 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
584 if (!volume_info->num) {
585 info_PK->start_pos = info_TL->end_header + 1;
587 info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1;
589 info_PK->end_pos = info_PK->start_pos + e - 1;
599 /* don't forget to release pi */
600 pi_destroy(pi, cp, tileno);
609 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
610 unsigned char *c = src;
611 opj_pi_iterator_t *pi;
615 opj_volume_t *volume = t2->volume;
616 opj_cp_t *cp = t2->cp;
618 /* create a packet iterator */
619 pi = pi_create(volume, cp, tileno);
621 /* TODO: throw an error */
625 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
626 while (pi_next(&pi[pino])) {
627 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
628 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
633 /* progression in resolution */
634 for (i = 0; i < 3; i++){
635 volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) : volume->comps[pi[pino].compno].resno_decoded[i];
639 if (e == -999) { /* ADD */
642 opj_event_msg(t2->cinfo, EVT_INFO, " t2_decode_packet: %d bytes decoded\n",e);
648 /* don't forget to release pi */
649 pi_destroy(pi, cp, tileno);
658 /* ----------------------------------------------------------------------- */
660 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp) {
661 /* create the tcd structure */
662 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
671 void t2_destroy(opj_t2_t *t2) {