09c184ac674b85d5bd607b4a99de7cfaf7ba33bb
[openjpeg.git] / libopenjpeg / tcd.h
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef __TCD_H
32 #define __TCD_H
33 /**
34 @file tcd.h
35 @brief Implementation of a tile coder/decoder (TCD)
36
37 The functions in TCD.C have for goal to encode or decode each tile independently from
38 each other. The functions in TCD.C are used by some function in J2K.C.
39 */
40
41 /** @defgroup TCD TCD - Implementation of a tile coder/decoder */
42 /*@{*/
43
44 /**
45 FIXME: documentation
46 */
47 //typedef struct opj_tcd_seg {
48 //  unsigned char** data;
49 //  int dataindex;
50 //  int numpasses;
51 //  int len;
52 //  int maxpasses;
53 //  int numnewpasses;
54 //  int newlen;
55 //} opj_tcd_seg_t;
56
57 /**
58 FIXME: documentation
59 */
60 typedef struct opj_tcd_seg {
61   OPJ_BYTE ** data;
62   OPJ_UINT32 dataindex;
63   OPJ_UINT32 numpasses;
64   OPJ_UINT32 real_num_passes;
65   OPJ_UINT32 len;
66   OPJ_UINT32 maxpasses;
67   OPJ_UINT32 numnewpasses;
68   OPJ_UINT32 newlen;
69 } opj_tcd_seg_t;
70
71 /**
72 FIXME: documentation
73 */
74 typedef struct opj_tcd_pass {
75   int rate;
76   double distortiondec;
77   int term, len;
78 } opj_tcd_pass_t;
79
80 /**
81 FIXME: documentation
82 */
83 typedef struct opj_tcd_layer {
84   int numpasses;                /* Number of passes in the layer */
85   int len;                      /* len of information */
86   double disto;                 /* add for index (Cfr. Marcela) */
87   unsigned char *data;          /* data */
88 } opj_tcd_layer_t;
89
90 /**
91 FIXME: documentation
92 */
93 typedef struct opj_tcd_cblk_enc {
94   unsigned char* data;  /* Data */
95   opj_tcd_layer_t* layers;      /* layer information */
96   opj_tcd_pass_t* passes;       /* information about the passes */
97   int x0, y0, x1, y1;           /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
98   int numbps;
99   int numlenbits;
100   int numpasses;                /* number of pass already done for the code-blocks */
101   int numpassesinlayers;        /* number of passes in the layer */
102   int totalpasses;              /* total number of passes */
103 } opj_tcd_cblk_enc_t;
104
105 //typedef struct opj_tcd_cblk_dec {
106 //  unsigned char* data;        /* Data */
107 //  opj_tcd_seg_t* segs;                /* segments informations */
108 //      int x0, y0, x1, y1;             /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
109 //  int numbps;
110 //  int numlenbits;
111 //  int len;                    /* length */
112 //  int numnewpasses;           /* number of pass added to the code-blocks */
113 //  int numsegs;                        /* number of segments */
114 //} opj_tcd_cblk_dec_t;
115
116 typedef struct opj_tcd_cblk_dec {
117   OPJ_BYTE * data;      /* Data */
118   opj_tcd_seg_t* segs;          /* segments informations */
119   OPJ_INT32 x0, y0, x1, y1;             /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
120   OPJ_UINT32 numbps;
121   OPJ_UINT32 numlenbits;
122   OPJ_UINT32 len;                       /* length */
123   OPJ_UINT32 numnewpasses;              /* number of pass added to the code-blocks */
124   OPJ_UINT32 numsegs;                   /* number of segments */
125   OPJ_UINT32 real_num_segs;
126   OPJ_UINT32 m_current_max_segs;
127 } opj_tcd_cblk_dec_t;
128
129 /**
130 FIXME: documentation
131 */
132 //typedef struct opj_tcd_precinct {
133 //  int x0, y0, x1, y1;         /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
134 //  int cw, ch;                 /* number of precinct in width and heigth */
135 //  union{              /* code-blocks informations */
136 //        opj_tcd_cblk_enc_t* enc;
137 //        opj_tcd_cblk_dec_t* dec;
138 //  } cblks;
139 //  opj_tgt_tree_t *incltree;           /* inclusion tree */
140 //  opj_tgt_tree_t *imsbtree;           /* IMSB tree */
141 //} opj_tcd_precinct_t;
142
143
144 typedef struct opj_tcd_precinct {
145   OPJ_INT32 x0, y0, x1, y1;             /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
146   OPJ_UINT32 cw, ch;                    /* number of precinct in width and heigth */
147   union{                /* code-blocks informations */
148           opj_tcd_cblk_enc_t* enc;
149           opj_tcd_cblk_dec_t* dec;
150   } cblks;
151   OPJ_UINT32 block_size;                /* size taken by cblks (in bytes) */
152   struct opj_tgt_tree *incltree;                /* inclusion tree */
153  struct opj_tgt_tree *imsbtree;         /* IMSB tree */
154 } opj_tcd_precinct_t;
155
156 /**
157 FIXME: documentation
158 */
159 //typedef struct opj_tcd_band {
160 //  int x0, y0, x1, y1;         /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
161 //  int bandno;
162 //  opj_tcd_precinct_t *precincts;      /* precinct information */
163 //  int numbps;
164 //  float stepsize;
165 //} opj_tcd_band_t;
166
167 typedef struct opj_tcd_band {
168   OPJ_INT32 x0, y0, x1, y1;             /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
169   OPJ_UINT32 bandno;
170   opj_tcd_precinct_t *precincts;        /* precinct information */
171   OPJ_UINT32 precincts_data_size;               /* size of data taken by precincts */
172   OPJ_INT32 numbps;
173   OPJ_FLOAT32 stepsize;
174 } opj_tcd_band_t;
175
176 /**
177 FIXME: documentation
178 */
179 typedef struct opj_tcd_resolution {
180   int x0, y0, x1, y1;           /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
181   int pw, ph;
182   int numbands;                 /* number sub-band for the resolution level */
183   opj_tcd_band_t bands[3];              /* subband information */
184 } opj_tcd_resolution_t;
185
186 /**
187 FIXME: documentation
188 */
189 //typedef struct opj_tcd_tilecomp {
190 //  int x0, y0, x1, y1;         /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
191 //  int numresolutions;         /* number of resolutions level */
192 //  opj_tcd_resolution_t *resolutions;  /* resolutions information */
193 //  int *data;                  /* data of the component */
194 //  int numpix;                 /* add fixed_quality */
195 //} opj_tcd_tilecomp_t;
196
197 typedef struct opj_tcd_tilecomp
198 {
199   OPJ_INT32 x0, y0, x1, y1;             /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
200   OPJ_UINT32 numresolutions;            /* number of resolutions level */
201   OPJ_UINT32 minimum_num_resolutions;           /* number of resolutions level to decode (at max)*/
202   opj_tcd_resolution_t *resolutions;    /* resolutions information */
203   OPJ_UINT32 resolutions_size;          /* size of data for resolutions (in bytes) */
204   OPJ_INT32 *data;                      /* data of the component */
205   OPJ_UINT32 data_size;                 /* size of the data of the component */
206   OPJ_INT32 numpix;                     /* add fixed_quality */
207 } opj_tcd_tilecomp_t;
208
209
210 /**
211 FIXME: documentation
212 */
213 typedef struct opj_tcd_tile {
214   int x0, y0, x1, y1;           /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
215   int numcomps;                 /* number of components in tile */
216   opj_tcd_tilecomp_t *comps;    /* Components information */
217   int numpix;                   /* add fixed_quality */
218   double distotile;             /* add fixed_quality */
219   double distolayer[100];       /* add fixed_quality */
220   /** packet number */
221   int packno;
222 } opj_tcd_tile_t;
223
224 /**
225 FIXME: documentation
226 */
227 typedef struct opj_tcd_image {
228   int tw, th;                   /* number of tiles in width and heigth */
229   opj_tcd_tile_t *tiles;                /* Tiles information */
230 } opj_tcd_image_t;
231
232 /**
233 Tile coder/decoder
234 */
235 typedef struct opj_tcd {
236         /** Position of the tilepart flag in Progression order*/
237         int tp_pos;
238         /** Tile part number*/
239         int tp_num;
240         /** Current tile part number*/
241         int cur_tp_num;
242         /** Total number of tileparts of the current tile*/
243         int cur_totnum_tp;
244         /** Current Packet iterator number */
245         int cur_pino;
246         /** codec context */
247         opj_common_ptr cinfo;
248
249         /** info on each image tile */
250         opj_tcd_image_t *tcd_image;
251         /** image */
252         opj_image_t *image;
253         /** coding parameters */
254         opj_cp_t *cp;
255         /** pointer to the current encoded/decoded tile */
256         opj_tcd_tile_t *tcd_tile;
257         /** coding/decoding parameters common to all tiles */
258         opj_tcp_t *tcp;
259         /** current encoded/decoded tile */
260         int tcd_tileno;
261         /** Time taken to encode a tile*/
262         double encoding_time;
263 } opj_tcd_t;
264
265
266 struct opj_image;
267 struct opj_cp_v2;
268 struct opj_tcp_v2;
269 /**
270 Tile coder/decoder
271 */
272 typedef struct opj_tcd_v2
273 {
274         /** Position of the tilepart flag in Progression order*/
275         OPJ_INT32 tp_pos;
276         /** Tile part number*/
277         OPJ_UINT32 tp_num;
278         /** Current tile part number*/
279         OPJ_UINT32 cur_tp_num;
280         /** Total number of tileparts of the current tile*/
281         OPJ_UINT32 cur_totnum_tp;
282         /** Current Packet iterator number */
283         OPJ_UINT32 cur_pino;
284         /** info on each image tile */
285         opj_tcd_image_t *tcd_image;
286         /** image header */
287         struct opj_image_header *image_header;
288         /** coding parameters */
289         struct opj_cp_v2 *cp;
290         /** coding/decoding parameters common to all tiles */
291         struct opj_tcp_v2 *tcp;
292         /** current encoded/decoded tile */
293         OPJ_UINT32 tcd_tileno;
294         /** tell if the tcd is a decoder. */
295         OPJ_UINT32 m_is_decoder : 1;
296 } opj_tcd_v2_t;
297
298 /** @name Exported functions */
299 /*@{*/
300 /* ----------------------------------------------------------------------- */
301
302 /**
303 Dump the content of a tcd structure
304 */
305 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img);
306 /**
307 Create a new TCD handle
308 @param cinfo Codec context info
309 @return Returns a new TCD handle if successful returns NULL otherwise
310 */
311 opj_tcd_t* tcd_create(opj_common_ptr cinfo);
312
313 /**
314 Create a new TCD handle
315 @param FIXME
316 @return Returns a new TCD handle if successful returns NULL otherwise
317 */
318 opj_tcd_v2_t* tcd_create_v2(opj_bool p_is_decoder);
319
320 /**
321 Destroy a previously created TCD handle
322 @param tcd TCD handle to destroy
323 */
324 void tcd_destroy_v2(opj_tcd_v2_t *tcd);
325
326 /**
327  * Initialize the tile coder and may reuse some meory.
328  * @param       p_tcd           TCD handle.
329  * @param       p_image         raw image.
330  * @param       p_cp            coding parameters.
331  * @param       p_tile_no       current tile index to encode.
332  *
333  * @return true if the encoding values could be set (false otherwise).
334 */
335 opj_bool tcd_init_v2(
336                                                 opj_tcd_v2_t *p_tcd,
337                                                 //struct opj_image * p_image,
338                                                 opj_image_header_t * p_image_header,
339                                                 //struct opj_cp * p_cp
340                                                 opj_cp_v2_t * p_cp
341                                         );
342
343 /**
344  * Allocates memory for decoding a specific tile.
345  *
346  * @param       p_tcd           the tile decoder.
347  * @param       p_image         the image to decode.
348  * @param       p_cp            the decoding parameters.
349  * @param       p_tile_no       the index of the tile received in sequence. This not necesseraly lead to the
350  * tile at index p_tile_no.
351  * @param       p_cstr_info     codestream info (if any).
352  *
353  * @return      true if the remaining data is sufficient.s
354  */
355 opj_bool tcd_init_decode_tile(
356                                                         opj_tcd_v2_t *p_tcd,
357                                                         OPJ_UINT32 p_tile_no
358                                                         );
359
360 /**
361 Destroy a previously created TCD handle
362 @param tcd TCD handle to destroy
363 */
364 void tcd_destroy(opj_tcd_t *tcd);
365 /**
366 Initialize the tile coder (allocate the memory)
367 @param tcd TCD handle
368 @param image Raw image
369 @param cp Coding parameters
370 @param curtileno Number that identifies the tile that will be encoded
371 */
372 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
373 /**
374 Free the memory allocated for encoding
375 @param tcd TCD handle
376 */
377 void tcd_free_encode(opj_tcd_t *tcd);
378 /**
379 Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
380 @param tcd TCD handle
381 @param image Raw image
382 @param cp Coding parameters
383 @param curtileno Number that identifies the tile that will be encoded
384 */
385 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
386 /**
387 Initialize the tile decoder
388 @param tcd TCD handle
389 @param image Raw image
390 @param cp Coding parameters
391 */
392 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp);
393 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info);
394 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final);
395 void tcd_rateallocate_fixed(opj_tcd_t *tcd);
396 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final);
397 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info);
398 /**
399 Encode a tile from the raw image into a buffer
400 @param tcd TCD handle
401 @param tileno Number that identifies one of the tiles to be encoded
402 @param dest Destination buffer
403 @param len Length of destination buffer
404 @param cstr_info Codestream information structure 
405 @return 
406 */
407 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info);
408 /**
409 Decode a tile from a buffer into a raw image
410 @param tcd TCD handle
411 @param src Source buffer
412 @param len Length of source buffer
413 @param tileno Number that identifies one of the tiles to be decoded
414 @param cstr_info Codestream information structure
415 */
416 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info);
417 /**
418 Free the memory allocated for decoding
419 @param tcd TCD handle
420 */
421 void tcd_free_decode(opj_tcd_t *tcd);
422 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno);
423
424
425 /**
426  * Gets the maximum tile size that will be taken by the tile once decoded.
427  */
428 OPJ_UINT32 tcd_get_decoded_tile_size (
429                                                  opj_tcd_v2_t *p_tcd
430                                                  );
431
432 /**
433 Decode a tile from a buffer into a raw image
434 @param tcd TCD handle
435 @param src Source buffer
436 @param len Length of source buffer
437 @param tileno Number that identifies one of the tiles to be decoded
438 */
439 opj_bool tcd_decode_tile_v2(opj_tcd_v2_t *tcd, OPJ_BYTE *src, OPJ_UINT32 len, OPJ_UINT32 tileno, struct opj_codestream_info *cstr_info);
440
441
442 /**
443  * Copies tile data from the system onto the given memory block.
444  */
445 opj_bool tcd_update_tile_data (
446                                                  opj_tcd_v2_t *p_tcd,
447                                                  OPJ_BYTE * p_dest,
448                                                  OPJ_UINT32 p_dest_length
449                                                  );
450
451 /* ----------------------------------------------------------------------- */
452 /*@}*/
453
454 /*@}*/
455
456 #endif /* __TCD_H */