JasPer  2.0.29
jpc_mqenc.h
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  * British Columbia.
4  * Copyright (c) 2001-2002 Michael David Adams.
5  * All rights reserved.
6  */
7 
8 /* __START_OF_JASPER_LICENSE__
9  *
10  * JasPer License Version 2.0
11  *
12  * Copyright (c) 2001-2006 Michael David Adams
13  * Copyright (c) 1999-2000 Image Power, Inc.
14  * Copyright (c) 1999-2000 The University of British Columbia
15  *
16  * All rights reserved.
17  *
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  *
26  * 1. The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  *
30  * 2. The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  *
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  *
61  * __END_OF_JASPER_LICENSE__
62  */
63 
64 /*
65  * MQ Arithmetic Encoder
66  *
67  * $Id$
68  */
69 
70 #ifndef JPC_MQENC_H
71 #define JPC_MQENC_H
72 
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76 
77 #include "jasper/jas_types.h"
78 #include "jasper/jas_stream.h"
79 
80 #include "jpc_mqcod.h"
81 
82 #include <stdio.h>
83 
84 /******************************************************************************\
85 * Constants.
86 \******************************************************************************/
87 
88 /*
89  * Termination modes.
90  */
91 
92 #define JPC_MQENC_DEFTERM 0 /* default termination */
93 #define JPC_MQENC_PTERM 1 /* predictable termination */
94 
95 /******************************************************************************\
96 * Types.
97 \******************************************************************************/
98 
99 /* MQ arithmetic encoder class. */
100 
101 typedef struct {
102 
103  /* The C register. */
104  uint_least32_t creg;
105 
106  /* The A register. */
107  uint_least32_t areg;
108 
109  /* The CT register. */
110  uint_least32_t ctreg;
111 
112  /* The maximum number of contexts. */
113  unsigned maxctxs;
114 
115  /* The per-context information. */
116  const jpc_mqstate_t **ctxs;
117 
118  /* The current context. */
119  const jpc_mqstate_t **curctx;
120 
121  /* The stream for encoder output. */
122  jas_stream_t *out;
123 
124  /* The byte buffer (i.e., the B variable in the standard). */
125  int_least16_t outbuf;
126 
127  /* The last byte output. */
128  int_least16_t lastbyte;
129 
130  /* The error indicator. */
131  bool err;
132 
133 } jpc_mqenc_t;
134 
135 /* MQ arithmetic encoder state information. */
136 
137 typedef struct {
138 
139  /* The A register. */
140  unsigned areg;
141 
142  /* The C register. */
143  unsigned creg;
144 
145  /* The CT register. */
146  unsigned ctreg;
147 
148  /* The last byte output by the encoder. */
149  int lastbyte;
150 
151 } jpc_mqencstate_t;
152 
153 /******************************************************************************\
154 * Functions/macros for construction and destruction.
155 \******************************************************************************/
156 
157 /* Create a MQ encoder. */
158 jpc_mqenc_t *jpc_mqenc_create(unsigned maxctxs, jas_stream_t *out);
159 
160 /* Destroy a MQ encoder. */
161 void jpc_mqenc_destroy(jpc_mqenc_t *enc);
162 
163 /******************************************************************************\
164 * Functions/macros for initialization.
165 \******************************************************************************/
166 
167 /* Initialize a MQ encoder. */
168 void jpc_mqenc_init(jpc_mqenc_t *enc);
169 
170 /******************************************************************************\
171 * Functions/macros for context manipulation.
172 \******************************************************************************/
173 
174 /* Set the current context. */
175 static inline void jpc_mqenc_setcurctx(jpc_mqenc_t *enc, unsigned ctxno) {
176  enc->curctx = &enc->ctxs[ctxno];
177 }
178 
179 /* Set the state information for multiple contexts. */
180 void jpc_mqenc_setctxs(jpc_mqenc_t *enc, unsigned numctxs, const jpc_mqctx_t *ctxs);
181 
182 /******************************************************************************\
183 * Miscellaneous functions/macros.
184 \******************************************************************************/
185 
186 /* Get the error state of a MQ encoder. */
187 static inline bool jpc_mqenc_error(const jpc_mqenc_t *enc) {
188  return enc->err;
189 }
190 
191 /* Get the current encoder state. */
192 void jpc_mqenc_getstate(const jpc_mqenc_t *enc, jpc_mqencstate_t *state);
193 
194 /* Terminate the code. */
195 int jpc_mqenc_flush(jpc_mqenc_t *enc, int termmode);
196 
197 /******************************************************************************\
198 * Functions/macros for encoding bits.
199 \******************************************************************************/
200 
201 /******************************************************************************\
202 * Functions/macros for debugging.
203 \******************************************************************************/
204 
205 int jpc_mqenc_dump(const jpc_mqenc_t *mqenc, FILE *out);
206 
207 /******************************************************************************\
208 * Implementation-specific details.
209 \******************************************************************************/
210 
211 /* Note: These function prototypes are included only to satisfy the
212  needs of the mqenc_putbit_macro macro. Do not call any of these
213  functions directly. */
214 int jpc_mqenc_codemps2(jpc_mqenc_t *enc);
215 int jpc_mqenc_codelps(jpc_mqenc_t *enc);
216 
217 int jpc_mqenc_putbit(jpc_mqenc_t *enc, bool bit);
218 
219 #endif
I/O Stream Class.
Primitive Types.