2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
41 /** @name Local static functions */
44 static void t2_putcommacode(opj_bio_t *bio, int n);
45 static int t2_getcommacode(opj_bio_t *bio);
47 Variable length code for signalling delta Zil (truncation point)
48 @param bio Bit Input/Output component
51 static void t2_putnumpasses(opj_bio_t *bio, int n);
52 static int t2_getnumpasses(opj_bio_t *bio);
54 Encode a packet of a tile to a destination buffer
55 @param tile Tile for which to write the packets
56 @param tcp Tile coding parameters
57 @param pi Packet identity
58 @param dest Destination buffer
59 @param len Length of the destination buffer
60 @param volume_info Structure to create an index file
61 @param tileno Number of the tile encoded
62 @param cp Coding parameters
63 @return Number of bytes encoded from the packet
65 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);
67 Initialize the segment decoder
68 @param seg Segment instance
69 @param cblksty Codeblock style
70 @param first Is first segment
72 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
74 Decode a packet of a tile from a source buffer
76 @param src Source buffer
77 @param len Length of the source buffer
78 @param tile Tile for which to write the packets
79 @param tcp Tile coding parameters
80 @param pi Packet identity
81 @return Number of bytes decoded from the packet
83 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);
89 /* ----------------------------------------------------------------------- */
91 /* #define RESTART 0x04 */
92 static void t2_putcommacode(opj_bio_t *bio, int n) {
99 static int t2_getcommacode(opj_bio_t *bio) {
101 for (n = 0; bio_read(bio, 1); n++) {
107 static void t2_putnumpasses(opj_bio_t *bio, int n) {
109 bio_write(bio, 0, 1);
111 bio_write(bio, 2, 2);
113 bio_write(bio, 0xc | (n - 3), 4);
114 } else if (n <= 36) {
115 bio_write(bio, 0x1e0 | (n - 6), 9);
116 } else if (n <= 164) {
117 bio_write(bio, 0xff80 | (n - 37), 16);
121 static int t2_getnumpasses(opj_bio_t *bio) {
123 if (!bio_read(bio, 1))
125 if (!bio_read(bio, 1))
127 if ((n = bio_read(bio, 2)) != 3)
129 if ((n = bio_read(bio, 5)) != 31)
131 return (37 + bio_read(bio, 7));
134 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) {
136 unsigned char *sop = 0, *eph = 0;
137 unsigned char *c = dest;
139 int compno = pi->compno; /* component value */
140 int resno = pi->resno; /* resolution level value */
141 int precno = pi->precno; /* precinct value */
142 int layno = pi->layno; /* quality layer value */
144 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
145 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
147 opj_bio_t *bio = NULL; /* BIO component */
150 if ((tcp->csty & J3D_CP_CSTY_SOP)) {
151 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
156 sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ;
157 sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 65536) % 256 ;
165 for (bandno = 0; bandno < res->numbands; bandno++) {
166 opj_tcd_band_t *band = &res->bands[bandno];
167 opj_tcd_precinct_t *prc = &band->precincts[precno];
168 tgt_reset(prc->incltree);
169 tgt_reset(prc->imsbtree);
170 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
171 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
173 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
179 bio_init_enc(bio, c, len);
180 bio_write(bio, 1, 1); /* Empty header bit */
182 /* Writing Packet header */
183 for (bandno = 0; bandno < res->numbands; bandno++) {
184 opj_tcd_band_t *band = &res->bands[bandno];
185 opj_tcd_precinct_t *prc = &band->precincts[precno];
186 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
187 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
188 opj_tcd_layer_t *layer = &cblk->layers[layno];
189 if (!cblk->numpasses && layer->numpasses) {
190 tgt_setvalue(prc->incltree, cblkno, layno);
194 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
195 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
196 opj_tcd_layer_t *layer = &cblk->layers[layno];
200 /* cblk inclusion bits */
201 if (!cblk->numpasses) {
202 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
204 bio_write(bio, layer->numpasses != 0, 1);
206 /* if cblk not included, go to the next cblk */
207 if (!layer->numpasses) {
210 /* if first instance of cblk --> zero bit-planes information */
211 if (!cblk->numpasses) {
212 cblk->numlenbits = 3;
213 tgt_encode(bio, prc->imsbtree, cblkno, 999);
215 /* number of coding passes included */
216 t2_putnumpasses(bio, layer->numpasses);
218 /* computation of the increase of the length indicator and insertion in the header */
219 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
220 opj_tcd_pass_t *pass = &cblk->passes[passno];
223 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
224 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
229 t2_putcommacode(bio, increment);
231 /* computation of the new Length indicator */
232 cblk->numlenbits += increment;
234 /* insertion of the codeword segment length */
235 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
236 opj_tcd_pass_t *pass = &cblk->passes[passno];
239 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
240 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
250 if (bio_flush(bio)) {
251 return -999; /* modified to eliminate longjmp !! */
254 c += bio_numbytes(bio);
259 if (tcp->csty & J3D_CP_CSTY_EPH) {
260 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
269 /* Writing the packet body */
271 for (bandno = 0; bandno < res->numbands; bandno++) {
272 opj_tcd_band_t *band = &res->bands[bandno];
273 opj_tcd_precinct_t *prc = &band->precincts[precno];
274 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
275 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
276 opj_tcd_layer_t *layer = &cblk->layers[layno];
277 if (!layer->numpasses) {
280 if (c + layer->len > dest + len) {
284 memcpy(c, layer->data, layer->len);
285 cblk->numpasses += layer->numpasses;
287 /* ADD for index Cfr. Marcela --> delta disto by packet */
288 if(volume_info && volume_info->index_write && volume_info->index_on) {
289 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
290 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
291 info_PK->disto += layer->disto;
292 if (volume_info->D_max < info_PK->disto) {
293 volume_info->D_max = info_PK->disto;
303 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
306 if (cblksty & J3D_CCP_CBLKSTY_TERMALL) {
309 else if (cblksty & J3D_CCP_CBLKSTY_LAZY) {
313 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
316 seg->maxpasses = 109;
320 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) {
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_tilecomp_t *tilec = &tile->comps[compno];
332 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
334 unsigned char *hd = NULL;
337 opj_bio_t *bio = NULL; /* BIO component */
340 for (bandno = 0; bandno < res->numbands; bandno++) {
341 opj_tcd_band_t *band = &res->bands[bandno];
342 opj_tcd_precinct_t *prc = &band->precincts[precno];
344 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
346 tgt_reset(prc->incltree);
347 tgt_reset(prc->imsbtree);
348 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
349 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
357 if (tcp->csty & J3D_CP_CSTY_SOP) {
358 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
359 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
364 /** TODO : check the Nsop value */
368 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
369 This part deal with this caracteristic
370 step 1: Read packet header in the saved structure
371 step 2: Return to codestream for decoding
376 if (cp->ppm == 1) { /* PPM */
378 bio_init_dec(bio, hd, cp->ppm_len);
379 } else if (tcp->ppt == 1) { /* PPT */
381 bio_init_dec(bio, hd, tcp->ppt_len);
382 } else { /* Normal Case */
384 bio_init_dec(bio, hd, src+len-hd);
387 present = bio_read(bio, 1);
391 hd += bio_numbytes(bio);
396 if (tcp->csty & J3D_CP_CSTY_EPH) {
397 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
398 printf("Error : expected EPH marker\n");
404 if (cp->ppm == 1) { /* PPM case */
405 cp->ppm_len += cp->ppm_data-hd;
409 if (tcp->ppt == 1) { /* PPT case */
410 tcp->ppt_len+=tcp->ppt_data-hd;
418 for (bandno = 0; bandno < res->numbands; bandno++) {
419 opj_tcd_band_t *band = &res->bands[bandno];
420 opj_tcd_precinct_t *prc = &band->precincts[precno];
422 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
424 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
425 int included, increment, n;
426 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
427 opj_tcd_seg_t *seg = NULL;
428 /* if cblk not yet included before --> inclusion tagtree */
429 if (!cblk->numsegs) {
430 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
433 included = bio_read(bio, 1);
435 /* if cblk not included */
437 cblk->numnewpasses = 0;
440 /* if cblk not yet included --> zero-bitplane tagtree */
441 if (!cblk->numsegs) {
443 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++);
445 cblk->numbps = band->numbps - numimsbs;
446 cblk->numlenbits = 3;
448 /* number of coding passes */
449 cblk->numnewpasses = t2_getnumpasses(bio);
450 increment = t2_getcommacode(bio);
451 /* length indicator increment */
452 cblk->numlenbits += increment;
453 if (!cblk->numsegs) {
454 seg = &cblk->segs[0];
455 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
457 seg = &cblk->segs[cblk->numsegs - 1];
458 if (seg->numpasses == seg->maxpasses) {
459 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
462 n = cblk->numnewpasses;
465 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
466 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
467 n -= seg->numnewpasses;
469 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
475 if (bio_inalign(bio)) {
480 hd += bio_numbytes(bio);
484 if (tcp->csty & J3D_CP_CSTY_EPH) {
485 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
486 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
494 cp->ppm_len+=cp->ppm_data-hd;
496 } else if (tcp->ppt == 1) {
497 tcp->ppt_len+=tcp->ppt_data-hd;
503 for (bandno = 0; bandno < res->numbands; bandno++) {
504 opj_tcd_band_t *band = &res->bands[bandno];
505 opj_tcd_precinct_t *prc = &band->precincts[precno];
507 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue;
509 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
510 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
511 opj_tcd_seg_t *seg = NULL;
512 if (!cblk->numnewpasses)
514 if (!cblk->numsegs) {
515 seg = &cblk->segs[0];
519 seg = &cblk->segs[cblk->numsegs - 1];
520 if (seg->numpasses == seg->maxpasses) {
527 if (c + seg->newlen > src + len) {
531 memcpy(cblk->data + cblk->len, c, seg->newlen);
532 if (seg->numpasses == 0) {
533 seg->data = cblk->data + cblk->len;
536 cblk->len += seg->newlen;
537 seg->len += seg->newlen;
538 seg->numpasses += seg->numnewpasses;
539 cblk->numnewpasses -= seg->numnewpasses;
540 if (cblk->numnewpasses > 0) {
544 } while (cblk->numnewpasses > 0);
551 /* ----------------------------------------------------------------------- */
553 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) {
554 unsigned char *c = dest;
556 opj_pi_iterator_t *pi = NULL;
559 opj_volume_t *volume = t2->volume;
560 opj_cp_t *cp = t2->cp;
562 /* create a packet iterator */
563 pi = pi_create(volume, cp, tileno);
565 fprintf(stdout,"[ERROR] Failed to create a pi structure\n");
570 volume_info->num = 0;
573 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
574 while (pi_next(&pi[pino])) {
575 if (pi[pino].layno < maxlayers) {
576 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, volume_info, tileno, cp);
577 /*opj_event_msg(t2->cinfo, EVT_INFO, " t2_encode_packet: %d bytes coded\n",e);*/
585 if(volume_info && volume_info->index_on) {
586 if(volume_info->index_write) {
587 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
588 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
589 if (!volume_info->num) {
590 info_PK->start_pos = info_TL->end_header + 1;
592 info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1;
594 info_PK->end_pos = info_PK->start_pos + e - 1;
604 /* don't forget to release pi */
605 pi_destroy(pi, cp, tileno);
614 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
615 unsigned char *c = src;
616 opj_pi_iterator_t *pi;
620 opj_volume_t *volume = t2->volume;
621 opj_cp_t *cp = t2->cp;
623 /* create a packet iterator */
624 pi = pi_create(volume, cp, tileno);
626 /* TODO: throw an error */
630 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
631 while (pi_next(&pi[pino])) {
632 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
633 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
638 /* progression in resolution */
639 for (i = 0; i < 3; i++){
640 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];
644 if (e == -999) { /* ADD */
647 opj_event_msg(t2->cinfo, EVT_INFO, " t2_decode_packet: %d bytes decoded\n",e);
653 /* don't forget to release pi */
654 pi_destroy(pi, cp, tileno);
663 /* ----------------------------------------------------------------------- */
665 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp) {
666 /* create the tcd structure */
667 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
676 void t2_destroy(opj_t2_t *t2) {