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,
66 opj_pi_iterator_t *pi, unsigned char *dest, int len,
67 opj_volume_info_t *volume_info, int tileno, opj_cp_t *cp);
69 Initialize the segment decoder
70 @param seg Segment instance
71 @param cblksty Codeblock style
72 @param first Is first segment
74 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
76 Decode a packet of a tile from a source buffer
78 @param src Source buffer
79 @param len Length of the source buffer
80 @param tile Tile for which to write the packets
81 @param tcp Tile coding parameters
82 @param pi Packet identity
83 @return Number of bytes decoded from the packet
85 int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len,
86 opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi);
92 /* ----------------------------------------------------------------------- */
94 /* #define RESTART 0x04 */
95 static void t2_putcommacode(opj_bio_t *bio, int n)
100 bio_write(bio, 0, 1);
103 static int t2_getcommacode(opj_bio_t *bio)
106 for (n = 0; bio_read(bio, 1); n++) {
112 static void t2_putnumpasses(opj_bio_t *bio, int n)
115 bio_write(bio, 0, 1);
117 bio_write(bio, 2, 2);
119 bio_write(bio, 0xc | (n - 3), 4);
120 } else if (n <= 36) {
121 bio_write(bio, 0x1e0 | (n - 6), 9);
122 } else if (n <= 164) {
123 bio_write(bio, 0xff80 | (n - 37), 16);
127 static int t2_getnumpasses(opj_bio_t *bio)
130 if (!bio_read(bio, 1)) {
133 if (!bio_read(bio, 1)) {
136 if ((n = bio_read(bio, 2)) != 3) {
139 if ((n = bio_read(bio, 5)) != 31) {
142 return (37 + bio_read(bio, 7));
145 static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp,
146 opj_pi_iterator_t *pi, unsigned char *dest, int len,
147 opj_volume_info_t * volume_info, int tileno, opj_cp_t *cp)
150 unsigned char *sop = 0, *eph = 0;
151 unsigned char *c = dest;
153 int compno = pi->compno; /* component value */
154 int resno = pi->resno; /* resolution level value */
155 int precno = pi->precno; /* precinct value */
156 int layno = pi->layno; /* quality layer value */
158 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
159 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
161 opj_bio_t *bio = NULL; /* BIO component */
164 if ((tcp->csty & J3D_CP_CSTY_SOP)) {
165 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
170 sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ;
171 sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 65536) % 256 ;
179 for (bandno = 0; bandno < res->numbands; bandno++) {
180 opj_tcd_band_t *band = &res->bands[bandno];
181 opj_tcd_precinct_t *prc = &band->precincts[precno];
182 tgt_reset(prc->incltree);
183 tgt_reset(prc->imsbtree);
184 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
186 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
188 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
194 bio_init_enc(bio, c, len);
195 bio_write(bio, 1, 1); /* Empty header bit */
197 /* Writing Packet header */
198 for (bandno = 0; bandno < res->numbands; bandno++) {
199 opj_tcd_band_t *band = &res->bands[bandno];
200 opj_tcd_precinct_t *prc = &band->precincts[precno];
201 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
203 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
204 opj_tcd_layer_t *layer = &cblk->layers[layno];
205 if (!cblk->numpasses && layer->numpasses) {
206 tgt_setvalue(prc->incltree, cblkno, layno);
210 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
212 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
213 opj_tcd_layer_t *layer = &cblk->layers[layno];
217 /* cblk inclusion bits */
218 if (!cblk->numpasses) {
219 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
221 bio_write(bio, layer->numpasses != 0, 1);
223 /* if cblk not included, go to the next cblk */
224 if (!layer->numpasses) {
227 /* if first instance of cblk --> zero bit-planes information */
228 if (!cblk->numpasses) {
229 cblk->numlenbits = 3;
230 tgt_encode(bio, prc->imsbtree, cblkno, 999);
232 /* number of coding passes included */
233 t2_putnumpasses(bio, layer->numpasses);
235 /* computation of the increase of the length indicator and insertion in the header */
236 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses;
238 opj_tcd_pass_t *pass = &cblk->passes[passno];
241 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
242 increment = int_max(increment,
243 int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
248 t2_putcommacode(bio, increment);
250 /* computation of the new Length indicator */
251 cblk->numlenbits += increment;
253 /* insertion of the codeword segment length */
254 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses;
256 opj_tcd_pass_t *pass = &cblk->passes[passno];
259 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
260 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
270 if (bio_flush(bio)) {
271 return -999; /* modified to eliminate longjmp !! */
274 c += bio_numbytes(bio);
279 if (tcp->csty & J3D_CP_CSTY_EPH) {
280 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
289 /* Writing the packet body */
291 for (bandno = 0; bandno < res->numbands; bandno++) {
292 opj_tcd_band_t *band = &res->bands[bandno];
293 opj_tcd_precinct_t *prc = &band->precincts[precno];
294 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
296 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
297 opj_tcd_layer_t *layer = &cblk->layers[layno];
298 if (!layer->numpasses) {
301 if (c + layer->len > dest + len) {
305 memcpy(c, layer->data, layer->len);
306 cblk->numpasses += layer->numpasses;
308 /* ADD for index Cfr. Marcela --> delta disto by packet */
309 if (volume_info && volume_info->index_write && volume_info->index_on) {
310 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
311 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
312 info_PK->disto += layer->disto;
313 if (volume_info->D_max < info_PK->disto) {
314 volume_info->D_max = info_PK->disto;
324 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first)
328 if (cblksty & J3D_CCP_CBLKSTY_TERMALL) {
330 } else if (cblksty & J3D_CCP_CBLKSTY_LAZY) {
334 seg->maxpasses = (((seg - 1)->maxpasses == 1) ||
335 ((seg - 1)->maxpasses == 10)) ? 2 : 1;
338 seg->maxpasses = 109;
342 int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len,
343 opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi)
346 unsigned char *c = src;
348 opj_cp_t *cp = t2->cp;
350 int compno = pi->compno; /* component value */
351 int resno = pi->resno; /* resolution level value */
352 int precno = pi->precno; /* precinct value */
353 int layno = pi->layno; /* quality layer value */
355 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
356 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
358 unsigned char *hd = NULL;
361 opj_bio_t *bio = NULL; /* BIO component */
364 for (bandno = 0; bandno < res->numbands; bandno++) {
365 opj_tcd_band_t *band = &res->bands[bandno];
366 opj_tcd_precinct_t *prc = &band->precincts[precno];
368 if ((band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0) ||
369 (band->z1 - band->z0 == 0)) {
373 tgt_reset(prc->incltree);
374 tgt_reset(prc->imsbtree);
375 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
377 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
385 if (tcp->csty & J3D_CP_CSTY_SOP) {
386 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
387 opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
392 /** TODO : check the Nsop value */
396 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
397 This part deal with this caracteristic
398 step 1: Read packet header in the saved structure
399 step 2: Return to codestream for decoding
404 if (cp->ppm == 1) { /* PPM */
406 bio_init_dec(bio, hd, cp->ppm_len);
407 } else if (tcp->ppt == 1) { /* PPT */
409 bio_init_dec(bio, hd, tcp->ppt_len);
410 } else { /* Normal Case */
412 bio_init_dec(bio, hd, src + len - hd);
415 present = bio_read(bio, 1);
419 hd += bio_numbytes(bio);
424 if (tcp->csty & J3D_CP_CSTY_EPH) {
425 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
426 printf("Error : expected EPH marker\n");
432 if (cp->ppm == 1) { /* PPM case */
433 cp->ppm_len += cp->ppm_data - hd;
437 if (tcp->ppt == 1) { /* PPT case */
438 tcp->ppt_len += tcp->ppt_data - hd;
446 for (bandno = 0; bandno < res->numbands; bandno++) {
447 opj_tcd_band_t *band = &res->bands[bandno];
448 opj_tcd_precinct_t *prc = &band->precincts[precno];
450 if ((band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0) ||
451 (band->z1 - band->z0 == 0)) {
455 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
457 int included, increment, n;
458 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
459 opj_tcd_seg_t *seg = NULL;
460 /* if cblk not yet included before --> inclusion tagtree */
461 if (!cblk->numsegs) {
462 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
465 included = bio_read(bio, 1);
467 /* if cblk not included */
469 cblk->numnewpasses = 0;
472 /* if cblk not yet included --> zero-bitplane tagtree */
473 if (!cblk->numsegs) {
475 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++);
477 cblk->numbps = band->numbps - numimsbs;
478 cblk->numlenbits = 3;
480 /* number of coding passes */
481 cblk->numnewpasses = t2_getnumpasses(bio);
482 increment = t2_getcommacode(bio);
483 /* length indicator increment */
484 cblk->numlenbits += increment;
485 if (!cblk->numsegs) {
486 seg = &cblk->segs[0];
487 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
489 seg = &cblk->segs[cblk->numsegs - 1];
490 if (seg->numpasses == seg->maxpasses) {
491 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
494 n = cblk->numnewpasses;
497 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
498 seg->newlen = bio_read(bio,
499 cblk->numlenbits + int_floorlog2(seg->numnewpasses));
500 n -= seg->numnewpasses;
502 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
508 if (bio_inalign(bio)) {
513 hd += bio_numbytes(bio);
517 if (tcp->csty & J3D_CP_CSTY_EPH) {
518 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
519 opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
527 cp->ppm_len += cp->ppm_data - hd;
529 } else if (tcp->ppt == 1) {
530 tcp->ppt_len += tcp->ppt_data - hd;
536 for (bandno = 0; bandno < res->numbands; bandno++) {
537 opj_tcd_band_t *band = &res->bands[bandno];
538 opj_tcd_precinct_t *prc = &band->precincts[precno];
540 if ((band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0) ||
541 (band->z1 - band->z0 == 0)) {
545 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
547 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
548 opj_tcd_seg_t *seg = NULL;
549 if (!cblk->numnewpasses) {
552 if (!cblk->numsegs) {
553 seg = &cblk->segs[0];
557 seg = &cblk->segs[cblk->numsegs - 1];
558 if (seg->numpasses == seg->maxpasses) {
565 if (c + seg->newlen > src + len) {
569 memcpy(cblk->data + cblk->len, c, seg->newlen);
570 if (seg->numpasses == 0) {
571 seg->data = cblk->data + cblk->len;
574 cblk->len += seg->newlen;
575 seg->len += seg->newlen;
576 seg->numpasses += seg->numnewpasses;
577 cblk->numnewpasses -= seg->numnewpasses;
578 if (cblk->numnewpasses > 0) {
582 } while (cblk->numnewpasses > 0);
589 /* ----------------------------------------------------------------------- */
591 int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile,
592 int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info)
594 unsigned char *c = dest;
596 opj_pi_iterator_t *pi = NULL;
599 opj_volume_t *volume = t2->volume;
600 opj_cp_t *cp = t2->cp;
602 /* create a packet iterator */
603 pi = pi_create(volume, cp, tileno);
605 fprintf(stdout, "[ERROR] Failed to create a pi structure\n");
610 volume_info->num = 0;
613 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
614 while (pi_next(&pi[pino])) {
615 if (pi[pino].layno < maxlayers) {
616 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c,
617 volume_info, tileno, cp);
618 /*opj_event_msg(t2->cinfo, EVT_INFO, " t2_encode_packet: %d bytes coded\n",e);*/
626 if (volume_info && volume_info->index_on) {
627 if (volume_info->index_write) {
628 opj_tile_info_t *info_TL = &volume_info->tile[tileno];
629 opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num];
630 if (!volume_info->num) {
631 info_PK->start_pos = info_TL->end_header + 1;
633 info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1;
635 info_PK->end_pos = info_PK->start_pos + e - 1;
645 /* don't forget to release pi */
646 pi_destroy(pi, cp, tileno);
655 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno,
656 opj_tcd_tile_t *tile)
658 unsigned char *c = src;
659 opj_pi_iterator_t *pi;
663 opj_volume_t *volume = t2->volume;
664 opj_cp_t *cp = t2->cp;
666 /* create a packet iterator */
667 pi = pi_create(volume, cp, tileno);
669 /* TODO: throw an error */
673 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
674 while (pi_next(&pi[pino])) {
675 if ((cp->layer == 0) || (cp->layer >= ((pi[pino].layno) + 1))) {
676 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
681 /* progression in resolution */
682 for (i = 0; i < 3; i++) {
683 volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(
684 pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) :
685 volume->comps[pi[pino].compno].resno_decoded[i];
689 if (e == -999) { /* ADD */
692 opj_event_msg(t2->cinfo, EVT_INFO, " t2_decode_packet: %d bytes decoded\n", e);
698 /* don't forget to release pi */
699 pi_destroy(pi, cp, tileno);
708 /* ----------------------------------------------------------------------- */
710 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp)
712 /* create the tcd structure */
713 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
724 void t2_destroy(opj_t2_t *t2)