2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2003, Yannick Verschueren
4 * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
44 extern jmp_buf j2k_error;
46 int t2_getcommacode() {
48 for (n=0; bio_read(1); n++) {}
55 if (!bio_read(1)) return 1;
56 if (!bio_read(1)) return 2;
57 if ((n=bio_read(2))!=3) return 3+n;
58 if ((n=bio_read(5))!=31) return 6+n;
59 return 37+bio_read(7);
62 void t2_init_seg(tcd_seg_t *seg, int cblksty) {
65 seg->maxpasses=cblksty&J2K_CCP_CBLKSTY_TERMALL?1:100;
68 int t2_decode_packet(unsigned char *src, int len, tcd_tile_t *tile, j2k_cp_t * cp, j2k_tcp_t *tcp, int compno, int resno, int precno, int layno, info_layer_t *layer_Idx) {
70 tcd_tilecomp_t *tilec = &tile->comps[compno];
71 tcd_resolution_t *res = &tilec->resolutions[resno];
72 unsigned char *c = src;
78 for (bandno = 0; bandno < res->numbands; bandno++) {
79 tcd_band_t *band = &res->bands[bandno];
80 tcd_precinct_t *prc = &band->precincts[precno];
81 tgt_reset(prc->incltree);
82 tgt_reset(prc->imsbtree);
83 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
84 tcd_cblk_t *cblk = &prc->cblks[cblkno];
90 layer_Idx->len_header = 0;
92 /* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
93 This part deal with this caracteristic
94 step 1: Read packet header in the saved structure
95 step 2: (futher) return to codestream for decoding */
96 if (cp->ppm == 1) /* PPM */
100 bio_init_dec(c, 1000);
103 if (tcp->ppt == 1) /* PPT */
107 bio_init_dec(c, 1000);
108 } else /* Normal Case */
110 if (tcp->csty & J2K_CP_CSTY_SOP)
112 if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [1]!!!\n");}
115 bio_init_dec(c, src + len - c);
116 layer_Idx->len_header = -6;
120 present = bio_read(1);
127 if (tcp->csty & J2K_CP_CSTY_EPH)
129 if ((*c) != 255 || (*(c+1) != 146)) {printf("Error : expected EPH marker [1]!!!\n");}
133 layer_Idx->len_header += (c-d);
135 /* PPT and PPM dealing */
136 if (cp->ppm == 1) /* PPM */
141 if (tcp->ppt == 1) /* PPT */
149 for (bandno=0; bandno<res->numbands; bandno++) {
150 tcd_band_t *band = &res->bands[bandno];
151 tcd_precinct_t *prc = &band->precincts[precno];
152 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
153 int included, increment, n;
154 tcd_cblk_t *cblk = &prc->cblks[cblkno];
156 if (!cblk->numsegs) {
157 included = tgt_decode(prc->incltree, cblkno, layno+1);
159 included = bio_read(1);
162 cblk->numnewpasses = 0;
165 if (!cblk->numsegs) {
167 for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {}
169 cblk->numbps = band->numbps - numimsbs;
170 cblk->numlenbits = 3;
172 cblk->numnewpasses = t2_getnumpasses();
173 increment = t2_getcommacode();
174 cblk->numlenbits += increment;
175 if (!cblk->numsegs) {
176 seg = &cblk->segs[0];
177 t2_init_seg(seg, tcp->tccps[compno].cblksty);
179 seg = &cblk->segs[cblk->numsegs - 1];
180 if (seg->numpasses == seg->maxpasses) {
181 t2_init_seg(++seg, tcp->tccps[compno].cblksty);
184 n = cblk->numnewpasses;
186 seg->numnewpasses = int_min(seg->maxpasses-seg->numpasses, n);
187 seg->newlen = bio_read(cblk->numlenbits + int_floorlog2(seg->numnewpasses));
188 n -= seg->numnewpasses;
190 t2_init_seg(++seg, tcp->tccps[compno].cblksty);
195 if(bio_inalign()) return -999;
198 if (tcp->csty & J2K_CP_CSTY_EPH) { /* EPH marker */
199 if ((*c) != 255 || (*(c+1) != 146)) {printf("Error : expected EPH marker [2]!!!\n"); }
204 layer_Idx->len_header += (c-d);
206 /* PPT Step 2 : see above for details */
209 cp->ppm_data = c; /* Update pointer */
212 layer_Idx->len_header = c-d;
216 if (tcp->csty & J2K_CP_CSTY_SOP)
218 if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [2] !!!\n"); }
221 bio_init_dec(c, src + len - c);
226 tcp->ppt_data = c; /* Update pointer */
228 layer_Idx->len_header = c-d;
232 if (tcp->csty & J2K_CP_CSTY_SOP) /* SOP marker */
234 if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [2] !!!\n"); }
237 bio_init_dec(c, src + len - c);
242 for (bandno = 0; bandno < res->numbands; bandno++) {
243 tcd_band_t *band = &res->bands[bandno];
244 tcd_precinct_t *prc = &band->precincts[precno];
245 for (cblkno = 0; cblkno < prc->cw*prc->ch; cblkno++) {
246 tcd_cblk_t *cblk = &prc->cblks[cblkno];
248 if (!cblk->numnewpasses) continue;
249 if (!cblk->numsegs) {
250 seg = &cblk->segs[cblk->numsegs++];
253 seg = &cblk->segs[cblk->numsegs-1];
254 if (seg->numpasses == seg->maxpasses) {
260 if (c + seg->newlen > src + len) return -999;
261 memcpy(cblk->data + cblk->len, c, seg->newlen);
262 if (seg->numpasses == 0) {
263 seg->data = cblk->data + cblk->len;
266 cblk->len += seg->newlen;
267 seg->len += seg->newlen;
268 seg->numpasses += seg->numnewpasses;
269 cblk->numnewpasses -= seg->numnewpasses;
270 if (cblk->numnewpasses > 0) {
274 } while (cblk->numnewpasses > 0);
285 void t2_init_info_packets(info_image_t *img, j2k_cp_t *cp)
287 int compno, tileno, resno, precno, layno;
289 for(compno = 0; compno < img->Comp; compno++)
291 for(tileno = 0; tileno < img->tw*img->th; tileno++)
293 info_tile_t *tile_Idx = &img->tile[tileno];
294 info_compo_t *compo_Idx = &tile_Idx->compo[compno];
295 for(resno = 0; resno < img->Decomposition + 1 ; resno++)
297 info_reso_t *reso_Idx = &compo_Idx->reso[resno];
298 for (precno = 0; precno < img->tile[tileno].pw * img->tile[tileno].ph; precno++)
300 info_prec_t *prec_Idx = &reso_Idx->prec[precno];
301 for(layno = 0; layno < img->Layer ; layno++)
303 info_layer_t *layer_Idx = &prec_Idx->layer[layno];
304 layer_Idx->offset = 0; /* start position */
305 layer_Idx->len_header = 0; /* length */
313 int t2_decode_packets(unsigned char *src, int len, j2k_image_t *img, j2k_cp_t *cp, int tileno, tcd_tile_t *tile, info_image_t *imgg) {
314 unsigned char *c = src;
318 info_tile_part_t *tile_part;
321 info_tile_t *tile_Idx;
322 info_compo_t *compo_Idx;
323 info_reso_t *reso_Idx;
324 info_prec_t *prec_Idx;
325 info_layer_t *layer_Idx;
327 t2_init_info_packets(imgg, cp); /* Initialize the packets information : LEN and OFFSET to 0 */
329 tile_Idx = &imgg->tile[tileno];
330 tile_Idx->num_packet = 0;
331 pi = pi_create(img, cp, tileno);
333 tile_part = &tile_Idx->tile_parts[partno];
334 position = tile_part->end_header + 1;
337 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++)
339 while (pi_next(&pi[pino]))
341 compo_Idx = &tile_Idx->compo[pi[pino].compno];
342 reso_Idx = &compo_Idx->reso[pi[pino].resno];
343 prec_Idx = &reso_Idx->prec[pi[pino].precno];
344 layer_Idx = &prec_Idx->layer[pi[pino].layno];
346 layer_Idx->offset = position;
347 layer_Idx->offset_header = position;
349 e = t2_decode_packet(c, src+len-c, tile, cp, &cp->tcps[tileno], pi[pino].compno, pi[pino].resno, pi[pino].precno, pi[pino].layno,layer_Idx);
357 /* Update position in case of multiple tile-parts for a tile >> */
359 if (length_read >= (tile_part->end_pos - tile_part->end_header))
362 tile_part = &tile_Idx->tile_parts[partno];
363 position = tile_part->end_header + 1;
368 tile_Idx->num_packet++;
371 // FREE space memory taken by pi
372 for (compno = 0; compno < pi[pino].numcomps; compno++)
374 free(pi[pino].comps[compno].resolutions);
376 free(pi[pino].comps);
386 imgg->num_packet_max=int_max(imgg->num_packet_max,tile_Idx->num_packet);