2fb739e99e757be9b6a617b6d78b03284aaabe76
[openjpeg.git] / libopenjpeg / t2.c
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5  * Copyright (c) 2005, Herv� Drolon, FreeImage Team
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "opj_includes.h"
32
33
34 /* #define RESTART 0x04 */
35
36 static void t2_putcommacode(opj_bio_t *bio, int n) {
37   while (--n >= 0) {
38     bio_write(bio, 1, 1);
39   }
40   bio_write(bio, 0, 1);
41 }
42
43 static int t2_getcommacode(opj_bio_t *bio) {
44   int n;
45   for (n = 0; bio_read(bio, 1); n++) {
46     ;
47   }
48   return n;
49 }
50
51 static void t2_putnumpasses(opj_bio_t *bio, int n) {
52   if (n == 1) {
53     bio_write(bio, 0, 1);
54   } else if (n == 2) {
55     bio_write(bio, 2, 2);
56   } else if (n <= 5) {
57     bio_write(bio, 0xc | (n - 3), 4);
58   } else if (n <= 36) {
59     bio_write(bio, 0x1e0 | (n - 6), 9);
60   } else if (n <= 164) {
61     bio_write(bio, 0xff80 | (n - 37), 16);
62   }
63 }
64
65 static int t2_getnumpasses(opj_bio_t *bio) {
66   int n;
67   if (!bio_read(bio, 1))
68     return 1;
69   if (!bio_read(bio, 1))
70     return 2;
71   if ((n = bio_read(bio, 2)) != 3)
72     return (3 + n);
73   if ((n = bio_read(bio, 5)) != 31)
74     return (6 + n);
75   return (37 + bio_read(bio, 7));
76 }
77
78 static int t2_encode_packet(opj_t2_t* t2, opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_image_info_t * image_info, int tileno) {
79   int bandno, cblkno;
80   unsigned char *sop = 0, *eph = 0;
81   unsigned char *c = dest;
82
83   int compno = pi->compno;  /* component value */
84   int resno  = pi->resno;   /* resolution level value */
85   int precno = pi->precno;  /* precinct value */
86   int layno  = pi->layno;   /* quality layer value */
87
88   opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
89   opj_tcd_resolution_t *res = &tilec->resolutions[resno];
90   
91   opj_bio_t *bio = NULL;  /* BIO component */
92   
93   /* <SOP 0xff91> */
94   if (tcp->csty & J2K_CP_CSTY_SOP) {
95     sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
96     sop[0] = 255;
97     sop[1] = 145;
98     sop[2] = 0;
99     sop[3] = 4;
100     sop[4] = (image_info->num % 65536) / 256;
101     sop[5] = (image_info->num % 65536) % 256;
102     memcpy(c, sop, 6);
103     opj_free(sop);
104     c += 6;
105   }
106   /* </SOP> */
107   
108   if (!layno) {
109     for (bandno = 0; bandno < res->numbands; bandno++) {
110       opj_tcd_band_t *band = &res->bands[bandno];
111       opj_tcd_precinct_t *prc = &band->precincts[precno];
112       tgt_reset(prc->incltree);
113       tgt_reset(prc->imsbtree);
114       for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
115         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
116         cblk->numpasses = 0;
117         tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
118       }
119     }
120   }
121   
122   bio = bio_create();
123   bio_init_enc(bio, c, len);
124   bio_write(bio, 1, 1);   /* Empty header bit */
125   
126   /* Writing Packet header */
127   for (bandno = 0; bandno < res->numbands; bandno++) {
128     opj_tcd_band_t *band = &res->bands[bandno];
129     opj_tcd_precinct_t *prc = &band->precincts[precno];
130     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
131       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
132       opj_tcd_layer_t *layer = &cblk->layers[layno];
133       if (!cblk->numpasses && layer->numpasses) {
134         tgt_setvalue(prc->incltree, cblkno, layno);
135       }
136     }
137     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
138       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
139       opj_tcd_layer_t *layer = &cblk->layers[layno];
140       int increment = 0;
141       int nump = 0;
142       int len = 0, passno;
143       /* cblk inclusion bits */
144       if (!cblk->numpasses) {
145         tgt_encode(bio, prc->incltree, cblkno, layno + 1);
146       } else {
147         bio_write(bio, layer->numpasses != 0, 1);
148       }
149       /* if cblk not included, go to the next cblk  */
150       if (!layer->numpasses) {
151         continue;
152       }
153       /* if first instance of cblk --> zero bit-planes information */
154       if (!cblk->numpasses) {
155         cblk->numlenbits = 3;
156         tgt_encode(bio, prc->imsbtree, cblkno, 999);
157       }
158       /* number of coding passes included */
159       t2_putnumpasses(bio, layer->numpasses);
160       
161       /* computation of the increase of the length indicator and insertion in the header     */
162       for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
163         opj_tcd_pass_t *pass = &cblk->passes[passno];
164         nump++;
165         len += pass->len;
166         if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
167           increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
168           len = 0;
169           nump = 0;
170         }
171       }
172       t2_putcommacode(bio, increment);
173
174       /* computation of the new Length indicator */
175       cblk->numlenbits += increment;
176
177       /* insertion of the codeword segment length */
178       for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
179         opj_tcd_pass_t *pass = &cblk->passes[passno];
180         nump++;
181         len += pass->len;
182         if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
183           bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
184           len = 0;
185           nump = 0;
186         }
187       }
188     }
189   }
190   
191   if (bio_flush(bio)) {
192     return -999;    /* modified to eliminate longjmp !! */
193   }
194   
195   c += bio_numbytes(bio);
196
197   bio_destroy(bio);
198   
199   /* <EPH 0xff92> */
200   if (tcp->csty & J2K_CP_CSTY_EPH) {
201     eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
202     eph[0] = 255;
203     eph[1] = 146;
204     memcpy(c, eph, 2);
205     opj_free(eph);
206     c += 2;
207   }
208   /* </EPH> */
209   
210   /* Writing the packet body */
211   
212   for (bandno = 0; bandno < res->numbands; bandno++) {
213     opj_tcd_band_t *band = &res->bands[bandno];
214     opj_tcd_precinct_t *prc = &band->precincts[precno];
215     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
216       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
217       opj_tcd_layer_t *layer = &cblk->layers[layno];
218       if (!layer->numpasses) {
219         continue;
220       }
221       if (c + layer->len > dest + len) {
222         return -999;
223       }
224       
225       memcpy(c, layer->data, layer->len);
226       cblk->numpasses += layer->numpasses;
227       c += layer->len;
228       /* ADD for index Cfr. Marcela --> delta disto by packet */
229       if(image_info && image_info->index_write && image_info->index_on) {
230         opj_tile_info_t *info_TL = &image_info->tile[tileno];
231         opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
232         info_PK->disto += layer->disto;
233         if (image_info->D_max < info_PK->disto) {
234           image_info->D_max = info_PK->disto;
235         }
236       }
237       /* </ADD> */
238     }
239   }
240   
241   return (c - dest);
242 }
243
244 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
245   seg->numpasses = 0;
246   seg->len = 0;
247   if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
248     seg->maxpasses = 1;
249   }
250   else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
251     if (first) {
252       seg->maxpasses = 10;
253     } else {
254       seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
255     }
256   } else {
257     seg->maxpasses = 109;
258   }
259 }
260
261 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) {
262   int bandno, cblkno;
263   unsigned char *c = src;
264
265   opj_cp_t *cp = t2->cp;
266
267   int compno = pi->compno;  /* component value */
268   int resno  = pi->resno;   /* resolution level value */
269   int precno = pi->precno;  /* precinct value */
270   int layno  = pi->layno;   /* quality layer value */
271
272   opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
273   opj_tcd_resolution_t *res = &tilec->resolutions[resno];
274   
275   unsigned char *hd = NULL;
276   int present;
277   
278   opj_bio_t *bio = NULL;  /* BIO component */
279   
280   if (layno == 0) {
281     for (bandno = 0; bandno < res->numbands; bandno++) {
282       opj_tcd_band_t *band = &res->bands[bandno];
283       opj_tcd_precinct_t *prc = &band->precincts[precno];
284       
285       if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
286       
287       tgt_reset(prc->incltree);
288       tgt_reset(prc->imsbtree);
289       for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
290         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
291         cblk->numsegs = 0;
292       }
293     }
294   }
295   
296   /* SOP markers */
297   
298   if (tcp->csty & J2K_CP_CSTY_SOP) {
299     if ((*c) != 0xff || (*(c + 1) != 0x91)) {
300       opg_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
301     } else {
302       c += 6;
303     }
304     
305     /** TODO : check the Nsop value */
306   }
307   
308   /* 
309   When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
310   This part deal with this caracteristic
311   step 1: Read packet header in the saved structure
312   step 2: Return to codestream for decoding 
313   */
314
315   bio = bio_create();
316   
317   if (cp->ppm == 1) {   /* PPM */
318     hd = cp->ppm_data;
319     bio_init_dec(bio, hd, cp->ppm_len);
320   } else if (tcp->ppt == 1) { /* PPT */
321     hd = tcp->ppt_data;
322     bio_init_dec(bio, hd, tcp->ppt_len);
323   } else {      /* Normal Case */
324     hd = c;
325     bio_init_dec(bio, hd, src+len-hd);
326   }
327   
328   present = bio_read(bio, 1);
329   
330   if (!present) {
331     bio_inalign(bio);
332     hd += bio_numbytes(bio);
333     bio_destroy(bio);
334     
335     /* EPH markers */
336     
337     if (tcp->csty & J2K_CP_CSTY_EPH) {
338       if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
339         printf("Error : expected EPH marker\n");
340       } else {
341         hd += 2;
342       }
343     }
344     
345     if (cp->ppm == 1) {   /* PPM case */
346       cp->ppm_len += cp->ppm_data-hd;
347       cp->ppm_data = hd;
348       return (c - src);
349     }
350     if (tcp->ppt == 1) {  /* PPT case */
351       tcp->ppt_len+=tcp->ppt_data-hd;
352       tcp->ppt_data = hd;
353       return (c - src);
354     }
355     
356     return (hd - src);
357   }
358   
359   for (bandno = 0; bandno < res->numbands; bandno++) {
360     opj_tcd_band_t *band = &res->bands[bandno];
361     opj_tcd_precinct_t *prc = &band->precincts[precno];
362     
363     if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
364     
365     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
366       int included, increment, n;
367       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
368       opj_tcd_seg_t *seg = NULL;
369       /* if cblk not yet included before --> inclusion tagtree */
370       if (!cblk->numsegs) {
371         included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
372         /* else one bit */
373       } else {
374         included = bio_read(bio, 1);
375       }
376       /* if cblk not included */
377       if (!included) {
378         cblk->numnewpasses = 0;
379         continue;
380       }
381       /* if cblk not yet included --> zero-bitplane tagtree */
382       if (!cblk->numsegs) {
383         int i, numimsbs;
384         for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
385           ;
386         }
387         numimsbs = i - 1;
388         cblk->numbps = band->numbps - numimsbs;
389         cblk->numlenbits = 3;
390       }
391       /* number of coding passes */
392       cblk->numnewpasses = t2_getnumpasses(bio);
393       increment = t2_getcommacode(bio);
394       /* length indicator increment */
395       cblk->numlenbits += increment;
396       if (!cblk->numsegs) {
397         seg = &cblk->segs[0];
398         t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
399       } else {
400         seg = &cblk->segs[cblk->numsegs - 1];
401         if (seg->numpasses == seg->maxpasses) {
402           t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
403         }
404       }
405       n = cblk->numnewpasses;
406       
407       do {
408         seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
409         seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
410         n -= seg->numnewpasses;
411         if (n > 0) {
412           t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
413         }
414       } while (n > 0);
415     }
416   }
417   
418   if (bio_inalign(bio)) {
419     bio_destroy(bio);
420     return -999;
421   }
422   
423   hd += bio_numbytes(bio);
424   bio_destroy(bio);
425   
426   /* EPH markers */
427   if (tcp->csty & J2K_CP_CSTY_EPH) {
428     if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
429       opg_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
430     } else {
431       hd += 2;
432     }
433   }
434   
435   if (cp->ppm==1) {
436     cp->ppm_len+=cp->ppm_data-hd;
437     cp->ppm_data = hd;
438   } else if (tcp->ppt == 1) {
439     tcp->ppt_len+=tcp->ppt_data-hd;
440     tcp->ppt_data = hd;
441   } else {
442     c=hd;
443   }
444   
445   for (bandno = 0; bandno < res->numbands; bandno++) {
446     opj_tcd_band_t *band = &res->bands[bandno];
447     opj_tcd_precinct_t *prc = &band->precincts[precno];
448     
449     if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
450     
451     for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
452       opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
453       opj_tcd_seg_t *seg = NULL;
454       if (!cblk->numnewpasses)
455         continue;
456       if (!cblk->numsegs) {
457         seg = &cblk->segs[0];
458         cblk->numsegs++;
459         cblk->len = 0;
460       } else {
461         seg = &cblk->segs[cblk->numsegs - 1];
462         if (seg->numpasses == seg->maxpasses) {
463           seg++;
464           cblk->numsegs++;
465         }
466       }
467       
468       do {
469         if (c + seg->newlen > src + len) {
470           return -999;
471         }
472         
473         memcpy(cblk->data + cblk->len, c, seg->newlen);
474         if (seg->numpasses == 0) {
475           seg->data = cblk->data + cblk->len;
476         }
477         c += seg->newlen;
478         cblk->len += seg->newlen;
479         seg->len += seg->newlen;
480         seg->numpasses += seg->numnewpasses;
481         cblk->numnewpasses -= seg->numnewpasses;
482         if (cblk->numnewpasses > 0) {
483           seg++;
484           cblk->numsegs++;
485         }
486       } while (cblk->numnewpasses > 0);
487     }
488   }
489   
490   return (c - src);
491 }
492
493 /* ----------------------------------------------------------------------- */
494
495 int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info) {
496   unsigned char *c = dest;
497   int e = 0;
498   opj_pi_iterator_t *pi = NULL;
499   int pino;
500
501   opj_image_t *image = t2->image;
502   opj_cp_t *cp = t2->cp;
503   
504   /* create a packet iterator */
505   pi = pi_create(image, cp, tileno);
506   if(!pi) {
507     /* TODO: throw an error */
508     return -999;
509   }
510   
511   for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
512     while (pi_next(&pi[pino])) {
513       if (pi[pino].layno < maxlayers) {
514         e = t2_encode_packet(t2, tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, image_info, tileno);
515         if (e == -999) {
516           break;
517         } else {
518           c += e;
519         }
520         
521         /* INDEX >> */
522         if(image_info && image_info->index_on) {
523           if(image_info->index_write) {
524             opj_tile_info_t *info_TL = &image_info->tile[tileno];
525             opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
526             if (!image_info->num) {
527               info_PK->start_pos = info_TL->end_header + 1;
528             } else {
529               info_PK->start_pos = info_TL->packet[image_info->num - 1].end_pos + 1;
530             }
531             info_PK->end_pos = info_PK->start_pos + e - 1;
532           }
533
534           image_info->num++;
535         }
536         /* << INDEX */
537       }
538     }
539   }
540
541   /* don't forget to release pi */
542   pi_destroy(pi, cp, tileno);
543   
544   if (e == -999) {
545     return e;
546   }
547
548     return (c - dest);
549 }
550
551 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
552   unsigned char *c = src;
553   opj_pi_iterator_t *pi;
554   int pino, e = 0;
555   int n = 0;
556
557   opj_image_t *image = t2->image;
558   opj_cp_t *cp = t2->cp;
559   
560   /* create a packet iterator */
561   pi = pi_create(image, cp, tileno);
562   if(!pi) {
563     /* TODO: throw an error */
564     return -999;
565   }
566   
567   for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
568     while (pi_next(&pi[pino])) {
569       if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
570         e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
571       } else {
572         e = 0;
573       }
574       
575       /* progression in resolution */
576       image->comps[pi[pino].compno].resno_decoded = 
577         (e > 0) ? 
578         int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
579         : image->comps[pi[pino].compno].resno_decoded;
580       n++;
581       
582       if (e == -999) {    /* ADD */
583         break;
584       } else {
585         c += e;
586       }
587     }
588   }
589
590   /* don't forget to release pi */
591   pi_destroy(pi, cp, tileno);
592   
593   if (e == -999) {
594     return e;
595   }
596   
597     return (c - src);
598 }
599
600 /* ----------------------------------------------------------------------- */
601
602 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
603   /* create the tcd structure */
604   opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
605   if(!t2) return NULL;
606   t2->cinfo = cinfo;
607   t2->image = image;
608   t2->cp = cp;
609
610   return t2;
611 }
612
613 void t2_destroy(opj_t2_t *t2) {
614   if(t2) {
615     opj_free(t2);
616   }
617 }
618