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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
35 @brief Implementation of an MQ-Coder (MQC)
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.
41 /** @defgroup MQC MQC - Implementation of an MQ-Coder */
45 This struct defines the state of a context.
47 typedef struct opj_mqc_state {
48 /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
50 /** the Most Probable Symbol (0 or 1) */
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;
58 #define MQC_NUMCTXS 32
63 typedef struct opj_mqc {
70 opj_mqc_state_t *ctxs[MQC_NUMCTXS];
71 opj_mqc_state_t **curctx;
74 /** @name Local static functions */
76 /* ----------------------------------------------------------------------- */
78 Output a byte, doing bit-stuffing if necessary.
79 After a 0xff byte, the next byte must be smaller than 0x90.
82 static void mqc_byteout(opj_mqc_t *mqc);
84 Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
87 static void mqc_renorme(opj_mqc_t *mqc);
89 Encode the most probable symbol
92 static void mqc_codemps(opj_mqc_t *mqc);
94 Encode the most least symbol
97 static void mqc_codelps(opj_mqc_t *mqc);
99 Fill mqc->c with 1's for flushing
100 @param mqc MQC handle
102 static void mqc_setbits(opj_mqc_t *mqc);
104 FIXME: documentation ???
105 @param mqc MQC handle
108 static int mqc_mpsexchange(opj_mqc_t *mqc);
110 FIXME: documentation ???
111 @param mqc MQC handle
114 static int mqc_lpsexchange(opj_mqc_t *mqc);
117 @param mqc MQC handle
119 static void mqc_bytein(opj_mqc_t *mqc);
121 Renormalize mqc->a and mqc->c while decoding
122 @param mqc MQC handle
124 static void mqc_renormd(opj_mqc_t *mqc);
125 /* ----------------------------------------------------------------------- */
128 /** @name Exported functions */
130 /* ----------------------------------------------------------------------- */
132 Create a new MQC handle
133 @return Returns a new MQC handle if successful, returns NULL otherwise
135 opj_mqc_t* mqc_create();
137 Destroy a previously created MQC handle
138 @param mqc MQC handle to destroy
140 void mqc_destroy(opj_mqc_t *mqc);
142 Return the number of bytes written/read since initialisation
143 @param mqc MQC handle
144 @return Returns the number of bytes already encoded
146 int mqc_numbytes(opj_mqc_t *mqc);
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
152 void mqc_resetstates(opj_mqc_t *mqc);
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
160 void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob);
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
166 void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp);
168 Set the current context used for coding/decoding
169 @param mqc MQC handle
170 @param ctxno Number that identifies the context
172 void mqc_setcurctx(opj_mqc_t *mqc, int ctxno);
174 Encode a symbol using the MQ-coder
175 @param mqc MQC handle
176 @param d The symbol to be encoded (0 or 1)
178 void mqc_encode(opj_mqc_t *mqc, int d);
180 Flush the encoder, so that all remaining data is written
181 @param mqc MQC handle
183 void mqc_flush(opj_mqc_t *mqc);
185 BYPASS mode switch, initialization operation.
187 <h2>Not fully implemented and tested !!</h2>
188 @param mqc MQC handle
190 void mqc_bypass_init_enc(opj_mqc_t *mqc);
192 BYPASS mode switch, coding operation.
194 <h2>Not fully implemented and tested !!</h2>
195 @param mqc MQC handle
196 @param d The symbol to be encoded (0 or 1)
198 void mqc_bypass_enc(opj_mqc_t *mqc, int d);
200 BYPASS mode switch, flush operation
201 <h2>Not fully implemented and tested !!</h2>
202 @param mqc MQC handle
203 @return Returns 1 (always)
205 int mqc_bypass_flush_enc(opj_mqc_t *mqc);
208 @param mqc MQC handle
210 void mqc_reset_enc(opj_mqc_t *mqc);
212 RESTART mode switch (TERMALL)
213 @param mqc MQC handle
214 @return Returns 1 (always)
216 int mqc_restart_enc(opj_mqc_t *mqc);
218 RESTART mode switch (TERMALL) reinitialisation
219 @param mqc MQC handle
221 void mqc_restart_init_enc(opj_mqc_t *mqc);
223 ERTERM mode switch (PTERM)
224 @param mqc MQC handle
226 void mqc_erterm_enc(opj_mqc_t *mqc);
228 SEGMARK mode switch (SEGSYM)
229 @param mqc MQC handle
231 void mqc_segmark_enc(opj_mqc_t *mqc);
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
238 void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len);
241 @param mqc MQC handle
242 @return Returns the decoded symbol (0 or 1)
244 int mqc_decode(opj_mqc_t *mqc);
245 /* ----------------------------------------------------------------------- */