7c601b039f985722f504bc635fe926c0dbf3750e
[openjpeg.git] / libopenjpeg / mqc.h
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 #ifndef __MQC_H
32 #define __MQC_H
33 /**
34 @file mqc.h
35 @brief Implementation of an MQ-Coder (MQC)
36
37 The functions in MQC.C have for goal to realize the MQ-coder operations. The functions
38 in MQC.C are used by some function in T1.C.
39 */
40
41 /** @defgroup MQC MQC - Implementation of an MQ-Coder */
42 /*@{*/
43
44 /**
45 This struct defines the state of a context.
46 */
47 typedef struct opj_mqc_state {
48   /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
49   unsigned int qeval;
50   /** the Most Probable Symbol (0 or 1) */
51   int mps;
52   /** next state if the next encoded symbol is the MPS */
53   struct opj_mqc_state *nmps;
54   /** next state if the next encoded symbol is the LPS */
55   struct opj_mqc_state *nlps;
56 } opj_mqc_state_t;
57
58 #define MQC_NUMCTXS 32
59
60 /**
61 MQ coder
62 */
63 typedef struct opj_mqc {
64   unsigned int c;
65   unsigned int a;
66   unsigned int ct;
67   unsigned char *bp;
68   unsigned char *start;
69   unsigned char *end;
70   opj_mqc_state_t *ctxs[MQC_NUMCTXS];
71   opj_mqc_state_t **curctx;
72 } opj_mqc_t;
73
74 /** @name Local static functions */
75 /*@{*/
76 /* ----------------------------------------------------------------------- */
77 /**
78 Output a byte, doing bit-stuffing if necessary.
79 After a 0xff byte, the next byte must be smaller than 0x90.
80 @param mqc MQC handle
81 */
82 static void mqc_byteout(opj_mqc_t *mqc);
83 /**
84 Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
85 @param mqc MQC handle
86 */
87 static void mqc_renorme(opj_mqc_t *mqc);
88 /**
89 Encode the most probable symbol
90 @param mqc MQC handle
91 */
92 static void mqc_codemps(opj_mqc_t *mqc);
93 /**
94 Encode the most least symbol
95 @param mqc MQC handle
96 */
97 static void mqc_codelps(opj_mqc_t *mqc);
98 /**
99 Fill mqc->c with 1's for flushing
100 @param mqc MQC handle
101 */
102 static void mqc_setbits(opj_mqc_t *mqc);
103 /**
104 FIXME: documentation ???
105 @param mqc MQC handle
106 @return 
107 */
108 static int mqc_mpsexchange(opj_mqc_t *mqc);
109 /**
110 FIXME: documentation ???
111 @param mqc MQC handle
112 @return 
113 */
114 static int mqc_lpsexchange(opj_mqc_t *mqc);
115 /**
116 Input a byte
117 @param mqc MQC handle
118 */
119 static void mqc_bytein(opj_mqc_t *mqc);
120 /**
121 Renormalize mqc->a and mqc->c while decoding
122 @param mqc MQC handle
123 */
124 static void mqc_renormd(opj_mqc_t *mqc);
125 /* ----------------------------------------------------------------------- */
126 /*@}*/
127
128 /** @name Exported functions */
129 /*@{*/
130 /* ----------------------------------------------------------------------- */
131 /**
132 Create a new MQC handle 
133 @return Returns a new MQC handle if successful, returns NULL otherwise
134 */
135 opj_mqc_t* mqc_create();
136 /**
137 Destroy a previously created MQC handle
138 @param mqc MQC handle to destroy
139 */
140 void mqc_destroy(opj_mqc_t *mqc);
141 /**
142 Return the number of bytes written/read since initialisation
143 @param mqc MQC handle
144 @return Returns the number of bytes already encoded
145 */
146 int mqc_numbytes(opj_mqc_t *mqc);
147 /**
148 Reset the states of all the context of the coder/decoder 
149 (each context is set to a state where 0 and 1 are more or less equiprobable)
150 @param mqc MQC handle
151 */
152 void mqc_resetstates(opj_mqc_t *mqc);
153 /**
154 Set the state of a particular context
155 @param mqc MQC handle
156 @param ctxno Number that identifies the context
157 @param msb The MSB of the new state of the context
158 @param prob Number that identifies the probability of the symbols for the new state of the context
159 */
160 void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob);
161 /**
162 Initialize the encoder
163 @param mqc MQC handle
164 @param bp Pointer to the start of the buffer where the bytes will be written
165 */
166 void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp);
167 /**
168 Set the current context used for coding/decoding
169 @param mqc MQC handle
170 @param ctxno Number that identifies the context
171 */
172 void mqc_setcurctx(opj_mqc_t *mqc, int ctxno);
173 /**
174 Encode a symbol using the MQ-coder
175 @param mqc MQC handle
176 @param d The symbol to be encoded (0 or 1)
177 */
178 void mqc_encode(opj_mqc_t *mqc, int d);
179 /**
180 Flush the encoder, so that all remaining data is written
181 @param mqc MQC handle
182 */
183 void mqc_flush(opj_mqc_t *mqc);
184 /**
185 BYPASS mode switch, initialization operation. 
186 JPEG 2000 p 505. 
187 <h2>Not fully implemented and tested !!</h2>
188 @param mqc MQC handle
189 */
190 void mqc_bypass_init_enc(opj_mqc_t *mqc);
191 /**
192 BYPASS mode switch, coding operation. 
193 JPEG 2000 p 505. 
194 <h2>Not fully implemented and tested !!</h2>
195 @param mqc MQC handle
196 @param d The symbol to be encoded (0 or 1)
197 */
198 void mqc_bypass_enc(opj_mqc_t *mqc, int d);
199 /**
200 BYPASS mode switch, flush operation
201 <h2>Not fully implemented and tested !!</h2>
202 @param mqc MQC handle
203 @return Returns 1 (always)
204 */
205 int mqc_bypass_flush_enc(opj_mqc_t *mqc);
206 /**
207 RESET mode switch
208 @param mqc MQC handle
209 */
210 void mqc_reset_enc(opj_mqc_t *mqc);
211 /**
212 RESTART mode switch (TERMALL)
213 @param mqc MQC handle
214 @return Returns 1 (always)
215 */
216 int mqc_restart_enc(opj_mqc_t *mqc);
217 /**
218 RESTART mode switch (TERMALL) reinitialisation
219 @param mqc MQC handle
220 */
221 void mqc_restart_init_enc(opj_mqc_t *mqc);
222 /**
223 ERTERM mode switch (PTERM)
224 @param mqc MQC handle
225 */
226 void mqc_erterm_enc(opj_mqc_t *mqc);
227 /**
228 SEGMARK mode switch (SEGSYM)
229 @param mqc MQC handle
230 */
231 void mqc_segmark_enc(opj_mqc_t *mqc);
232 /**
233 Initialize the decoder
234 @param mqc MQC handle
235 @param bp Pointer to the start of the buffer from which the bytes will be read
236 @param len Length of the input buffer
237 */
238 void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len);
239 /**
240 Decode a symbol
241 @param mqc MQC handle
242 @return Returns the decoded symbol (0 or 1)
243 */
244 int mqc_decode(opj_mqc_t *mqc);
245 /* ----------------------------------------------------------------------- */
246 /*@}*/
247
248 /*@}*/
249
250 #endif /* __MQC_H */