2 * Copyright (c) 2001-2003, David Janssens
\r
3 * Copyright (c) 2002-2003, Yannick Verschueren
\r
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
\r
5 * Copyright (c) 2005, Herv� Drolon, FreeImage Team
\r
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
\r
7 * All rights reserved.
\r
9 * Redistribution and use in source and binary forms, with or without
\r
10 * modification, are permitted provided that the following conditions
\r
12 * 1. Redistributions of source code must retain the above copyright
\r
13 * notice, this list of conditions and the following disclaimer.
\r
14 * 2. Redistributions in binary form must reproduce the above copyright
\r
15 * notice, this list of conditions and the following disclaimer in the
\r
16 * documentation and/or other materials provided with the distribution.
\r
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
\r
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
28 * POSSIBILITY OF SUCH DAMAGE.
\r
31 #include "opj_includes.h"
\r
33 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
\r
36 /** @name Local static functions */
\r
39 static void t2_putcommacode(opj_bio_t *bio, int n);
\r
40 static int t2_getcommacode(opj_bio_t *bio);
\r
42 Variable length code for signalling delta Zil (truncation point)
\r
43 @param bio Bit Input/Output component
\r
46 static void t2_putnumpasses(opj_bio_t *bio, int n);
\r
47 static int t2_getnumpasses(opj_bio_t *bio);
\r
49 Encode a packet of a tile to a destination buffer
\r
50 @param tile Tile for which to write the packets
\r
51 @param tcp Tile coding parameters
\r
52 @param pi Packet identity
\r
53 @param dest Destination buffer
\r
54 @param len Length of the destination buffer
\r
55 @param volume_info Structure to create an index file
\r
56 @param tileno Number of the tile encoded
\r
57 @param cp Coding parameters
\r
58 @return Number of bytes encoded from the packet
\r
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);
\r
62 Initialize the segment decoder
\r
63 @param seg Segment instance
\r
64 @param cblksty Codeblock style
\r
65 @param first Is first segment
\r
67 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
\r
69 Decode a packet of a tile from a source buffer
\r
71 @param src Source buffer
\r
72 @param len Length of the source buffer
\r
73 @param tile Tile for which to write the packets
\r
74 @param tcp Tile coding parameters
\r
75 @param pi Packet identity
\r
76 @return Number of bytes decoded from the packet
\r
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);
\r
84 /* ----------------------------------------------------------------------- */
\r
86 /* #define RESTART 0x04 */
\r
87 static void t2_putcommacode(opj_bio_t *bio, int n) {
\r
89 bio_write(bio, 1, 1);
\r
91 bio_write(bio, 0, 1);
\r
94 static int t2_getcommacode(opj_bio_t *bio) {
\r
96 for (n = 0; bio_read(bio, 1); n++) {
\r
102 static void t2_putnumpasses(opj_bio_t *bio, int n) {
\r
104 bio_write(bio, 0, 1);
\r
105 } else if (n == 2) {
\r
106 bio_write(bio, 2, 2);
\r
107 } else if (n <= 5) {
\r
108 bio_write(bio, 0xc | (n - 3), 4);
\r
109 } else if (n <= 36) {
\r
110 bio_write(bio, 0x1e0 | (n - 6), 9);
\r
111 } else if (n <= 164) {
\r
112 bio_write(bio, 0xff80 | (n - 37), 16);
\r
116 static int t2_getnumpasses(opj_bio_t *bio) {
\r
118 if (!bio_read(bio, 1))
\r
120 if (!bio_read(bio, 1))
\r
122 if ((n = bio_read(bio, 2)) != 3)
\r
124 if ((n = bio_read(bio, 5)) != 31)
\r
126 return (37 + bio_read(bio, 7));
\r
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) {
\r
130 int bandno, cblkno;
\r
131 unsigned char *sop = 0, *eph = 0;
\r
132 unsigned char *c = dest;
\r
134 int compno = pi->compno; /* component value */
\r
135 int resno = pi->resno; /* resolution level value */
\r
136 int precno = pi->precno; /* precinct value */
\r
137 int layno = pi->layno; /* quality layer value */
\r
139 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
\r
140 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
\r
142 opj_bio_t *bio = NULL; /* BIO component */
\r
145 if ((tcp->csty & J3D_CP_CSTY_SOP)) {
\r
146 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
\r
151 sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ;
\r
152 sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 65536) % 256 ;
\r
160 for (bandno = 0; bandno < res->numbands; bandno++) {
\r
161 opj_tcd_band_t *band = &res->bands[bandno];
\r
162 opj_tcd_precinct_t *prc = &band->precincts[precno];
\r
163 tgt_reset(prc->incltree);
\r
164 tgt_reset(prc->imsbtree);
\r
165 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
\r
166 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
167 cblk->numpasses = 0;
\r
168 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
\r
173 bio = bio_create();
\r
174 bio_init_enc(bio, c, len);
\r
175 bio_write(bio, 1, 1); /* Empty header bit */
\r
177 /* Writing Packet header */
\r
178 for (bandno = 0; bandno < res->numbands; bandno++) {
\r
179 opj_tcd_band_t *band = &res->bands[bandno];
\r
180 opj_tcd_precinct_t *prc = &band->precincts[precno];
\r
181 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
\r
182 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
183 opj_tcd_layer_t *layer = &cblk->layers[layno];
\r
184 if (!cblk->numpasses && layer->numpasses) {
\r
185 tgt_setvalue(prc->incltree, cblkno, layno);
\r
189 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
\r
190 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
191 opj_tcd_layer_t *layer = &cblk->layers[layno];
\r
194 int len = 0, passno;
\r
195 /* cblk inclusion bits */
\r
196 if (!cblk->numpasses) {
\r
197 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
\r
199 bio_write(bio, layer->numpasses != 0, 1);
\r
201 /* if cblk not included, go to the next cblk */
\r
202 if (!layer->numpasses) {
\r
205 /* if first instance of cblk --> zero bit-planes information */
\r
206 if (!cblk->numpasses) {
\r
207 cblk->numlenbits = 3;
\r
208 tgt_encode(bio, prc->imsbtree, cblkno, 999);
\r
210 /* number of coding passes included */
\r
211 t2_putnumpasses(bio, layer->numpasses);
\r
213 /* computation of the increase of the length indicator and insertion in the header */
\r
214 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
\r
215 opj_tcd_pass_t *pass = &cblk->passes[passno];
\r
218 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
\r
219 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
\r
224 t2_putcommacode(bio, increment);
\r
226 /* computation of the new Length indicator */
\r
227 cblk->numlenbits += increment;
\r
229 /* insertion of the codeword segment length */
\r
230 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
\r
231 opj_tcd_pass_t *pass = &cblk->passes[passno];
\r
234 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
\r
235 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
\r
245 if (bio_flush(bio)) {
\r
246 return -999; /* modified to eliminate longjmp !! */
\r
249 c += bio_numbytes(bio);
\r
254 if (tcp->csty & J3D_CP_CSTY_EPH) {
\r
255 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
\r
264 /* Writing the packet body */
\r
266 for (bandno = 0; bandno < res->numbands; bandno++) {
\r
267 opj_tcd_band_t *band = &res->bands[bandno];
\r
268 opj_tcd_precinct_t *prc = &band->precincts[precno];
\r
269 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
\r
270 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
271 opj_tcd_layer_t *layer = &cblk->layers[layno];
\r
272 if (!layer->numpasses) {
\r
275 if (c + layer->len > dest + len) {
\r
279 memcpy(c, layer->data, layer->len);
\r
280 cblk->numpasses += layer->numpasses;
\r
282 /* ADD for index Cfr. Marcela --> delta disto by packet */
\r
283 if(volume_info && volume_info->index_write && volume_info->index_on) {
\r
284 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
\r
285 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
\r
286 info_PK->disto += layer->disto;
\r
287 if (volume_info->D_max < info_PK->disto) {
\r
288 volume_info->D_max = info_PK->disto;
\r
298 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
\r
299 seg->numpasses = 0;
\r
301 if (cblksty & J3D_CCP_CBLKSTY_TERMALL) {
\r
302 seg->maxpasses = 1;
\r
304 else if (cblksty & J3D_CCP_CBLKSTY_LAZY) {
\r
306 seg->maxpasses = 10;
\r
308 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
\r
311 seg->maxpasses = 109;
\r
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) {
\r
316 int bandno, cblkno;
\r
317 unsigned char *c = src;
\r
319 opj_cp_t *cp = t2->cp;
\r
321 int compno = pi->compno; /* component value */
\r
322 int resno = pi->resno; /* resolution level value */
\r
323 int precno = pi->precno; /* precinct value */
\r
324 int layno = pi->layno; /* quality layer value */
\r
326 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
\r
327 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
\r
329 unsigned char *hd = NULL;
\r
332 opj_bio_t *bio = NULL; /* BIO component */
\r
335 for (bandno = 0; bandno < res->numbands; bandno++) {
\r
336 opj_tcd_band_t *band = &res->bands[bandno];
\r
337 opj_tcd_precinct_t *prc = &band->precincts[precno];
\r
339 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
\r
341 tgt_reset(prc->incltree);
\r
342 tgt_reset(prc->imsbtree);
\r
343 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
\r
344 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
352 if (tcp->csty & J3D_CP_CSTY_SOP) {
\r
353 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
\r
354 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
\r
359 /** TODO : check the Nsop value */
\r
363 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
\r
364 This part deal with this caracteristic
\r
365 step 1: Read packet header in the saved structure
\r
366 step 2: Return to codestream for decoding
\r
369 bio = bio_create();
\r
371 if (cp->ppm == 1) { /* PPM */
\r
373 bio_init_dec(bio, hd, cp->ppm_len);
\r
374 } else if (tcp->ppt == 1) { /* PPT */
\r
375 hd = tcp->ppt_data;
\r
376 bio_init_dec(bio, hd, tcp->ppt_len);
\r
377 } else { /* Normal Case */
\r
379 bio_init_dec(bio, hd, src+len-hd);
\r
382 present = bio_read(bio, 1);
\r
386 hd += bio_numbytes(bio);
\r
391 if (tcp->csty & J3D_CP_CSTY_EPH) {
\r
392 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
\r
393 printf("Error : expected EPH marker\n");
\r
399 if (cp->ppm == 1) { /* PPM case */
\r
400 cp->ppm_len += cp->ppm_data-hd;
\r
404 if (tcp->ppt == 1) { /* PPT case */
\r
405 tcp->ppt_len+=tcp->ppt_data-hd;
\r
406 tcp->ppt_data = hd;
\r
413 for (bandno = 0; bandno < res->numbands; bandno++) {
\r
414 opj_tcd_band_t *band = &res->bands[bandno];
\r
415 opj_tcd_precinct_t *prc = &band->precincts[precno];
\r
417 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
\r
419 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
\r
420 int included, increment, n;
\r
421 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
422 opj_tcd_seg_t *seg = NULL;
\r
423 /* if cblk not yet included before --> inclusion tagtree */
\r
424 if (!cblk->numsegs) {
\r
425 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
\r
428 included = bio_read(bio, 1);
\r
430 /* if cblk not included */
\r
432 cblk->numnewpasses = 0;
\r
435 /* if cblk not yet included --> zero-bitplane tagtree */
\r
436 if (!cblk->numsegs) {
\r
438 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++);
\r
440 cblk->numbps = band->numbps - numimsbs;
\r
441 cblk->numlenbits = 3;
\r
443 /* number of coding passes */
\r
444 cblk->numnewpasses = t2_getnumpasses(bio);
\r
445 increment = t2_getcommacode(bio);
\r
446 /* length indicator increment */
\r
447 cblk->numlenbits += increment;
\r
448 if (!cblk->numsegs) {
\r
449 seg = &cblk->segs[0];
\r
450 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
\r
452 seg = &cblk->segs[cblk->numsegs - 1];
\r
453 if (seg->numpasses == seg->maxpasses) {
\r
454 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
\r
457 n = cblk->numnewpasses;
\r
460 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
\r
461 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
\r
462 n -= seg->numnewpasses;
\r
464 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
\r
470 if (bio_inalign(bio)) {
\r
475 hd += bio_numbytes(bio);
\r
479 if (tcp->csty & J3D_CP_CSTY_EPH) {
\r
480 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
\r
481 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
\r
488 cp->ppm_len+=cp->ppm_data-hd;
\r
490 } else if (tcp->ppt == 1) {
\r
491 tcp->ppt_len+=tcp->ppt_data-hd;
\r
492 tcp->ppt_data = hd;
\r
497 for (bandno = 0; bandno < res->numbands; bandno++) {
\r
498 opj_tcd_band_t *band = &res->bands[bandno];
\r
499 opj_tcd_precinct_t *prc = &band->precincts[precno];
\r
501 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
\r
503 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
\r
504 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
505 opj_tcd_seg_t *seg = NULL;
\r
506 if (!cblk->numnewpasses)
\r
508 if (!cblk->numsegs) {
\r
509 seg = &cblk->segs[0];
\r
513 seg = &cblk->segs[cblk->numsegs - 1];
\r
514 if (seg->numpasses == seg->maxpasses) {
\r
521 if (c + seg->newlen > src + len) {
\r
525 memcpy(cblk->data + cblk->len, c, seg->newlen);
\r
526 if (seg->numpasses == 0) {
\r
527 seg->data = cblk->data + cblk->len;
\r
530 cblk->len += seg->newlen;
\r
531 seg->len += seg->newlen;
\r
532 seg->numpasses += seg->numnewpasses;
\r
533 cblk->numnewpasses -= seg->numnewpasses;
\r
534 if (cblk->numnewpasses > 0) {
\r
538 } while (cblk->numnewpasses > 0);
\r
545 /* ----------------------------------------------------------------------- */
\r
547 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) {
\r
548 unsigned char *c = dest;
\r
550 opj_pi_iterator_t *pi = NULL;
\r
553 opj_volume_t *volume = t2->volume;
\r
554 opj_cp_t *cp = t2->cp;
\r
556 /* create a packet iterator */
\r
557 pi = pi_create(volume, cp, tileno);
\r
559 fprintf(stdout,"[ERROR] Failed to create a pi structure\n");
\r
564 volume_info->num = 0;
\r
567 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
\r
568 while (pi_next(&pi[pino])) {
\r
569 if (pi[pino].layno < maxlayers) {
\r
570 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, volume_info, tileno, cp);
\r
571 //opj_event_msg(t2->cinfo, EVT_INFO, " t2_encode_packet: %d bytes coded\n",e);
\r
579 if(volume_info && volume_info->index_on) {
\r
580 if(volume_info->index_write) {
\r
581 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
\r
582 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
\r
583 if (!volume_info->num) {
\r
584 info_PK->start_pos = info_TL->end_header + 1;
\r
586 info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1;
\r
588 info_PK->end_pos = info_PK->start_pos + e - 1;
\r
591 volume_info->num++;
\r
598 /* don't forget to release pi */
\r
599 pi_destroy(pi, cp, tileno);
\r
608 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
\r
609 unsigned char *c = src;
\r
610 opj_pi_iterator_t *pi;
\r
614 opj_volume_t *volume = t2->volume;
\r
615 opj_cp_t *cp = t2->cp;
\r
617 /* create a packet iterator */
\r
618 pi = pi_create(volume, cp, tileno);
\r
620 /* TODO: throw an error */
\r
624 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
\r
625 while (pi_next(&pi[pino])) {
\r
626 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
\r
627 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
\r
632 /* progression in resolution */
\r
633 for (i = 0; i < 3; i++){
\r
634 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];
\r
638 if (e == -999) { /* ADD */
\r
641 opj_event_msg(t2->cinfo, EVT_INFO, " t2_decode_packet: %d bytes decoded\n",e);
\r
647 /* don't forget to release pi */
\r
648 pi_destroy(pi, cp, tileno);
\r
657 /* ----------------------------------------------------------------------- */
\r
659 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp) {
\r
660 /* create the tcd structure */
\r
661 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
\r
662 if(!t2) return NULL;
\r
664 t2->volume = volume;
\r
670 void t2_destroy(opj_t2_t *t2) {
\r