Fix potential out-of-bounds read (coverity) (#844)
[openjpeg.git] / src / lib / openmj2 / tcd.h
1 /*
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.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 #ifndef __TCD_H
38 #define __TCD_H
39 /**
40 @file tcd.h
41 @brief Implementation of a tile coder/decoder (TCD)
42
43 The functions in TCD.C have for goal to encode or decode each tile independently from
44 each other. The functions in TCD.C are used by some function in J2K.C.
45 */
46
47 /** @defgroup TCD TCD - Implementation of a tile coder/decoder */
48 /*@{*/
49
50 /**
51 FIXME: documentation
52 */
53 typedef struct opj_tcd_seg {
54   unsigned char** data;
55   int dataindex;
56   int numpasses;
57   int len;
58   int maxpasses;
59   int numnewpasses;
60   int newlen;
61 } opj_tcd_seg_t;
62
63 /**
64 FIXME: documentation
65 */
66 typedef struct opj_tcd_pass {
67   int rate;
68   double distortiondec;
69   int term, len;
70 } opj_tcd_pass_t;
71
72 /**
73 FIXME: documentation
74 */
75 typedef struct opj_tcd_layer {
76   int numpasses;                /* Number of passes in the layer */
77   int len;                      /* len of information */
78   double disto;                 /* add for index (Cfr. Marcela) */
79   unsigned char *data;          /* data */
80 } opj_tcd_layer_t;
81
82 /**
83 FIXME: documentation
84 */
85 typedef struct opj_tcd_cblk_enc {
86   unsigned char* data;  /* Data */
87   opj_tcd_layer_t* layers;      /* layer information */
88   opj_tcd_pass_t* passes;       /* information about the passes */
89   int x0, y0, x1, y1;           /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
90   int numbps;
91   int numlenbits;
92   int numpasses;                /* number of pass already done for the code-blocks */
93   int numpassesinlayers;        /* number of passes in the layer */
94   int totalpasses;              /* total number of passes */
95 } opj_tcd_cblk_enc_t;
96
97 typedef struct opj_tcd_cblk_dec {
98   unsigned char* data;  /* Data */
99   opj_tcd_seg_t* segs;          /* segments informations */
100         int x0, y0, x1, y1;             /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
101   int numbps;
102   int numlenbits;
103   int len;                      /* length */
104   int numnewpasses;             /* number of pass added to the code-blocks */
105   int numsegs;                  /* number of segments */
106 } opj_tcd_cblk_dec_t;
107
108 /**
109 FIXME: documentation
110 */
111 typedef struct opj_tcd_precinct {
112   int x0, y0, x1, y1;           /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
113   int cw, ch;                   /* number of precinct in width and heigth */
114   union{                /* code-blocks informations */
115           opj_tcd_cblk_enc_t* enc;
116           opj_tcd_cblk_dec_t* dec;
117   } cblks;
118   opj_tgt_tree_t *incltree;             /* inclusion tree */
119   opj_tgt_tree_t *imsbtree;             /* IMSB tree */
120 } opj_tcd_precinct_t;
121
122 /**
123 FIXME: documentation
124 */
125 typedef struct opj_tcd_band {
126   int x0, y0, x1, y1;           /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
127   int bandno;
128   opj_tcd_precinct_t *precincts;        /* precinct information */
129   int numbps;
130   float stepsize;
131 } opj_tcd_band_t;
132
133 /**
134 FIXME: documentation
135 */
136 typedef struct opj_tcd_resolution {
137   int x0, y0, x1, y1;           /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
138   int pw, ph;
139   int numbands;                 /* number sub-band for the resolution level */
140   opj_tcd_band_t bands[3];              /* subband information */
141 } opj_tcd_resolution_t;
142
143 /**
144 FIXME: documentation
145 */
146 typedef struct opj_tcd_tilecomp {
147   int x0, y0, x1, y1;           /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
148   int numresolutions;           /* number of resolutions level */
149   opj_tcd_resolution_t *resolutions;    /* resolutions information */
150   int *data;                    /* data of the component */
151   int numpix;                   /* add fixed_quality */
152 } opj_tcd_tilecomp_t;
153
154 /**
155 FIXME: documentation
156 */
157 typedef struct opj_tcd_tile {
158   int x0, y0, x1, y1;           /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
159   int numcomps;                 /* number of components in tile */
160   opj_tcd_tilecomp_t *comps;    /* Components information */
161   int numpix;                   /* add fixed_quality */
162   double distotile;             /* add fixed_quality */
163   double distolayer[100];       /* add fixed_quality */
164   /** packet number */
165   int packno;
166 } opj_tcd_tile_t;
167
168 /**
169 FIXME: documentation
170 */
171 typedef struct opj_tcd_image {
172   int tw, th;                   /* number of tiles in width and heigth */
173   opj_tcd_tile_t *tiles;                /* Tiles information */
174 } opj_tcd_image_t;
175
176 /**
177 Tile coder/decoder
178 */
179 typedef struct opj_tcd {
180         /** Position of the tilepart flag in Progression order*/
181         int tp_pos;
182         /** Tile part number*/
183         int tp_num;
184         /** Current tile part number*/
185         int cur_tp_num;
186         /** Total number of tileparts of the current tile*/
187         int cur_totnum_tp;
188         /** Current Packet iterator number */
189         int cur_pino;
190         /** codec context */
191         opj_common_ptr cinfo;
192
193         /** info on each image tile */
194         opj_tcd_image_t *tcd_image;
195         /** image */
196         opj_image_t *image;
197         /** coding parameters */
198         opj_cp_t *cp;
199         /** pointer to the current encoded/decoded tile */
200         opj_tcd_tile_t *tcd_tile;
201         /** coding/decoding parameters common to all tiles */
202         opj_tcp_t *tcp;
203         /** current encoded/decoded tile */
204         int tcd_tileno;
205         /** Time taken to encode a tile*/
206         double encoding_time;
207 } opj_tcd_t;
208
209 /** @name Exported functions */
210 /*@{*/
211 /* ----------------------------------------------------------------------- */
212
213 /**
214 Dump the content of a tcd structure
215 */
216 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img);
217 /**
218 Create a new TCD handle
219 @param cinfo Codec context info
220 @return Returns a new TCD handle if successful returns NULL otherwise
221 */
222 opj_tcd_t* tcd_create(opj_common_ptr cinfo);
223 /**
224 Destroy a previously created TCD handle
225 @param tcd TCD handle to destroy
226 */
227 void tcd_destroy(opj_tcd_t *tcd);
228 /**
229 Initialize the tile coder (allocate the memory)
230 @param tcd TCD handle
231 @param image Raw image
232 @param cp Coding parameters
233 @param curtileno Number that identifies the tile that will be encoded
234 */
235 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
236 /**
237 Free the memory allocated for encoding
238 @param tcd TCD handle
239 */
240 void tcd_free_encode(opj_tcd_t *tcd);
241 /**
242 Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
243 @param tcd TCD handle
244 @param image Raw image
245 @param cp Coding parameters
246 @param curtileno Number that identifies the tile that will be encoded
247 */
248 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
249 /**
250 Initialize the tile decoder
251 @param tcd TCD handle
252 @param image Raw image
253 @param cp Coding parameters
254 */
255 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp);
256 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);
257 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final);
258 void tcd_rateallocate_fixed(opj_tcd_t *tcd);
259 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final);
260 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info);
261 /**
262 Encode a tile from the raw image into a buffer
263 @param tcd TCD handle
264 @param tileno Number that identifies one of the tiles to be encoded
265 @param dest Destination buffer
266 @param len Length of destination buffer
267 @param cstr_info Codestream information structure 
268 @return 
269 */
270 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info);
271 /**
272 Decode a tile from a buffer into a raw image
273 @param tcd TCD handle
274 @param src Source buffer
275 @param len Length of source buffer
276 @param tileno Number that identifies one of the tiles to be decoded
277 @param cstr_info Codestream information structure
278 */
279 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info);
280 /**
281 Free the memory allocated for decoding
282 @param tcd TCD handle
283 */
284 void tcd_free_decode(opj_tcd_t *tcd);
285 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno);
286
287 /* ----------------------------------------------------------------------- */
288 /*@}*/
289
290 /*@}*/
291
292 #endif /* __TCD_H */