Avoided ABI breakage
[openjpeg.git] / indexer_JPIP / t2.c
1 /*
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
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28
29 #include "t2.h"
30 #include "tcd.h"
31 #include "bio.h"
32 #include "j2k.h"
33 #include "pi.h"
34 #include "tgt.h"
35 #include "int.h"
36 #include "cio.h"
37 #include <stdio.h>
38 #include <setjmp.h>
39 #include <string.h>
40 #include <stdlib.h> 
41
42 #define RESTART 0x04
43
44 extern jmp_buf j2k_error;
45
46 int t2_getcommacode() {
47     int n;
48     for (n=0; bio_read(1); n++) {}
49     return n;
50 }
51
52 int t2_getnumpasses()
53 {
54     int 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);
60 }
61
62 void t2_init_seg(tcd_seg_t *seg, int cblksty) {
63     seg->numpasses=0;
64     seg->len=0;
65     seg->maxpasses=cblksty&J2K_CCP_CBLKSTY_TERMALL?1:100;
66 }
67
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) {
69     int bandno, cblkno;
70     tcd_tilecomp_t *tilec = &tile->comps[compno];
71     tcd_resolution_t *res = &tilec->resolutions[resno];
72     unsigned char *c = src;
73     unsigned char *d = c;
74     int e;
75     int present;
76
77     if (layno == 0) {
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];
85                 cblk->numsegs = 0;
86             }
87         }  
88     }
89     /* INDEX */
90     layer_Idx->len_header = 0;
91
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 */
97       {     
98         c = cp->ppm_data;
99         d = c;
100         bio_init_dec(c, 1000);
101       } else 
102         {
103           if (tcp->ppt == 1) /* PPT */
104             {
105               c = tcp->ppt_data;
106               d = c;
107               bio_init_dec(c, 1000);
108             } else /* Normal Case */
109               {
110                 if (tcp->csty & J2K_CP_CSTY_SOP) 
111                   {
112                     if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [1]!!!\n");}
113                     c += 6;
114                   }
115                 bio_init_dec(c, src + len - c);
116                 layer_Idx->len_header = -6;
117               }
118         }
119     
120     present = bio_read(1);
121     
122     if (!present) 
123       {
124         bio_inalign();
125         /* Normal case */
126         c += bio_numbytes();
127         if (tcp->csty & J2K_CP_CSTY_EPH) 
128           {
129             if ((*c) != 255 || (*(c+1) != 146)) {printf("Error : expected EPH marker [1]!!!\n");}
130             c += 2;
131           }
132         /* INDEX */
133         layer_Idx->len_header += (c-d);
134
135         /* PPT and PPM dealing */
136         if (cp->ppm == 1) /* PPM */
137           {     
138             cp->ppm_data = c;       
139             return 0;
140           }
141         if (tcp->ppt == 1) /* PPT */
142           {
143             tcp->ppt_data = c;
144             return 0;
145           }
146         return c - src;
147       }
148     
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];
155             tcd_seg_t *seg;
156             if (!cblk->numsegs) {
157                 included = tgt_decode(prc->incltree, cblkno, layno+1);
158             } else {
159                 included = bio_read(1);
160             }
161             if (!included) {
162                 cblk->numnewpasses = 0;
163                 continue;
164             }
165             if (!cblk->numsegs) {
166                 int i, numimsbs;
167                 for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {}
168                 numimsbs = i-1;
169                 cblk->numbps = band->numbps - numimsbs;
170                 cblk->numlenbits = 3;
171             }
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);
178             } else {
179                 seg = &cblk->segs[cblk->numsegs - 1];
180                 if (seg->numpasses == seg->maxpasses) {
181                     t2_init_seg(++seg, tcp->tccps[compno].cblksty);
182                 }
183             }
184             n = cblk->numnewpasses;
185             do {
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;
189                 if (n > 0) {
190                     t2_init_seg(++seg, tcp->tccps[compno].cblksty);
191                 }
192             } while (n > 0);
193         }
194     }
195     if(bio_inalign()) return -999;
196     c += bio_numbytes();
197
198     if (tcp->csty & J2K_CP_CSTY_EPH) { /* EPH marker */
199       if ((*c) != 255 || (*(c+1) != 146)) {printf("Error : expected EPH marker [2]!!!\n"); }
200       c += 2;
201     }
202     
203     /* INDEX */
204     layer_Idx->len_header += (c-d);
205
206     /* PPT Step 2 : see above for details */
207     if (cp->ppm == 1)
208       {
209         cp->ppm_data = c; /* Update pointer */
210
211         /* INDEX */
212         layer_Idx->len_header = c-d;
213
214         c = src;
215         d = c;
216         if (tcp->csty & J2K_CP_CSTY_SOP) 
217           {
218             if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [2] !!!\n"); }
219             c += 6;
220           }
221         bio_init_dec(c, src + len - c);
222       } else 
223         {
224           if (tcp->ppt == 1)
225             { 
226               tcp->ppt_data = c; /* Update pointer */
227               /* INDEX */
228               layer_Idx->len_header = c-d;
229
230               c = src;
231               d = c;
232               if (tcp->csty & J2K_CP_CSTY_SOP) /* SOP marker */
233                 { 
234                   if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [2] !!!\n"); }
235                   c += 6;
236                 }
237               bio_init_dec(c, src + len - c);
238               
239             }
240         }
241
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];
247             tcd_seg_t *seg;
248             if (!cblk->numnewpasses) continue;
249             if (!cblk->numsegs) {
250                 seg = &cblk->segs[cblk->numsegs++];
251                 cblk->len = 0;
252             } else {
253                 seg = &cblk->segs[cblk->numsegs-1];
254                 if (seg->numpasses == seg->maxpasses) {
255                     seg++;
256                     cblk->numsegs++;
257                 }
258             }
259             do {
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;
264                 }
265                 c += seg->newlen;
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) {
271                     seg++;
272                     cblk->numsegs++;
273                 }
274             } while (cblk->numnewpasses > 0);
275         }
276     }
277     /* <INDEX> */
278     e = c-d;
279     layer_Idx->len = e;
280     /* </INDEX> */
281
282     return c-src;
283 }
284
285 void t2_init_info_packets(info_image_t *img, j2k_cp_t *cp)
286 {
287   int compno, tileno, resno, precno, layno;
288
289   for(compno = 0; compno < img->Comp; compno++)
290     {
291       for(tileno = 0; tileno < img->tw*img->th; tileno++)
292         {
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++)
296             {
297               info_reso_t *reso_Idx = &compo_Idx->reso[resno];
298               for (precno = 0; precno < img->tile[tileno].pw * img->tile[tileno].ph; precno++)
299                 {
300                   info_prec_t *prec_Idx = &reso_Idx->prec[precno];
301                   for(layno = 0; layno < img->Layer ; layno++)
302                     {
303                       info_layer_t *layer_Idx = &prec_Idx->layer[layno];
304                       layer_Idx->offset = 0;        /* start position */
305                       layer_Idx->len_header = 0;    /* length         */
306                     }
307                 }
308             }
309         }
310     }
311 }
312
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;
315     pi_iterator_t *pi;
316     int pino, compno,e;
317     int partno;
318     info_tile_part_t *tile_part;
319     int position;
320     int length_read;
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;
326
327     t2_init_info_packets(imgg, cp); /* Initialize the packets information : LEN and OFFSET to 0 */
328
329     tile_Idx = &imgg->tile[tileno];
330     tile_Idx->num_packet = 0;
331     pi = pi_create(img, cp, tileno);
332     partno = 0;
333     tile_part = &tile_Idx->tile_parts[partno];
334     position = tile_part->end_header + 1;
335     length_read = 0;
336
337     for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++)
338       {
339         while (pi_next(&pi[pino])) 
340           {   
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];
345             
346             layer_Idx->offset = position;
347             layer_Idx->offset_header = position;
348             
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);
350             if (e == -999)
351               {
352                 break;
353               } else
354                 c += e;
355             position += e;
356             
357             /* Update position in case of multiple tile-parts for a tile >> */
358             length_read += e;
359             if (length_read >= (tile_part->end_pos - tile_part->end_header))
360               {
361                 partno++;
362                 tile_part = &tile_Idx->tile_parts[partno];
363                 position = tile_part->end_header + 1;
364                 length_read = 0;
365               }
366             /* << end_update */
367             
368             tile_Idx->num_packet++;
369           }
370         
371         // FREE space memory taken by pi
372         for (compno = 0; compno < pi[pino].numcomps; compno++) 
373           { 
374             free(pi[pino].comps[compno].resolutions);
375           } 
376         free(pi[pino].comps);
377       }
378     
379     free(pi[0].include);
380     free(pi);
381  
382     if (e==-999)
383       return e;
384     else
385       {
386         imgg->num_packet_max=int_max(imgg->num_packet_max,tile_Idx->num_packet);
387         return c-src;
388       }
389 }