1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *   British Columbia.
4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Copyright (c) 2001-2003 Michael David Adams.
5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * All rights reserved.
6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* __START_OF_JASPER_LICENSE__
9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * JasPer License Version 2.0
11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Copyright (c) 2001-2006 Michael David Adams
13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Copyright (c) 1999-2000 Image Power, Inc.
14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Copyright (c) 1999-2000 The University of British Columbia
15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * All rights reserved.
17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * Permission is hereby granted, free of charge, to any person (the
19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * "User") obtaining a copy of this software and associated documentation
20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * files (the "Software"), to deal in the Software without restriction,
21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * including without limitation the rights to use, copy, modify, merge,
22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * publish, distribute, and/or sell copies of the Software, and to permit
23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * persons to whom the Software is furnished to do so, subject to the
24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * following conditions:
25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * 1.  The above copyright notices and this permission notice (which
27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * includes the disclaimer below) shall be included in all copies or
28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * substantial portions of the Software.
29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
30793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * 2.  The name of a copyright holder shall not be used to endorse or
31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * promote products derived from the Software without specific prior
32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * written permission.
33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * __END_OF_JASPER_LICENSE__
62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*
65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * MQ Arithmetic Decoder
66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler *
67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler * $Id: jpc_mqdec.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
68793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler */
69793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#ifndef JPC_MQDEC_H
71793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define JPC_MQDEC_H
72793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
73793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/******************************************************************************\
74793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* Includes.
75793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler\******************************************************************************/
76793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
77793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "jasper/jas_types.h"
78793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "jasper/jas_stream.h"
79793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
80793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "jpc_mqcod.h"
81793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
82793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/******************************************************************************\
83793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* Types.
84793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler\******************************************************************************/
85793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
86793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* MQ arithmetic decoder. */
87793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
88793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertypedef struct {
89793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
90793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The C register. */
91793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    uint_fast32_t creg;
92793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
93793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The A register. */
94793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    uint_fast32_t areg;
95793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
96793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The CT register. */
97793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    uint_fast32_t ctreg;
98793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
99793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The current context. */
100793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    jpc_mqstate_t **curctx;
101793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
102793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The per-context information. */
103793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    jpc_mqstate_t **ctxs;
104793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
105793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The maximum number of contexts. */
106793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int maxctxs;
107793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
108793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The stream from which to read data. */
109793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    jas_stream_t *in;
110793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
111793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The last character read. */
112793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    uchar inbuffer;
113793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
114793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    /* The EOF indicator. */
115793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int eof;
116793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
117793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} jpc_mqdec_t;
118793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
119793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/******************************************************************************\
120793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* Functions/macros for construction and destruction.
121793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler\******************************************************************************/
122793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
123793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Create a MQ decoder. */
124793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerjpc_mqdec_t *jpc_mqdec_create(int maxctxs, jas_stream_t *in);
125793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
126793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Destroy a MQ decoder. */
127793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid jpc_mqdec_destroy(jpc_mqdec_t *dec);
128793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
129793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/******************************************************************************\
130793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* Functions/macros for initialization.
131793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler\******************************************************************************/
132793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
133793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Set the input stream associated with a MQ decoder. */
134793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid jpc_mqdec_setinput(jpc_mqdec_t *dec, jas_stream_t *in);
135793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
136793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Initialize a MQ decoder. */
137793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid jpc_mqdec_init(jpc_mqdec_t *dec);
138793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
139793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/******************************************************************************\
140793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* Functions/macros for manipulating contexts.
141793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler\******************************************************************************/
142793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
143793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Set the current context for a MQ decoder. */
144793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_setcurctx(dec, ctxno) \
145793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ((mqdec)->curctx = &(mqdec)->ctxs[ctxno]);
146793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
147793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Set the state information for a particular context of a MQ decoder. */
148793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid jpc_mqdec_setctx(jpc_mqdec_t *dec, int ctxno, jpc_mqctx_t *ctx);
149793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
150793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Set the state information for all contexts of a MQ decoder. */
151793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid jpc_mqdec_setctxs(jpc_mqdec_t *dec, int numctxs, jpc_mqctx_t *ctxs);
152793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
153793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/******************************************************************************\
154793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* Functions/macros for decoding bits.
155793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler\******************************************************************************/
156793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
157793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Decode a symbol. */
158793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#if !defined(DEBUG)
159793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_getbit(dec) \
160793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    jpc_mqdec_getbit_macro(dec)
161793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#else
162793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_getbit(dec) \
163793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    jpc_mqdec_getbit_func(dec)
164793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
165793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
166793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Decode a symbol (assuming an unskewed probability distribution). */
167793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#if !defined(DEBUG)
168793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_getbitnoskew(dec) \
169793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    jpc_mqdec_getbit_macro(dec)
170793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#else
171793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_getbitnoskew(dec) \
172793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    jpc_mqdec_getbit_func(dec)
173793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
174793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
175793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/******************************************************************************\
176793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* Functions/macros for debugging.
177793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler\******************************************************************************/
178793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
179793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/* Dump the MQ decoder state for debugging. */
180793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid jpc_mqdec_dump(jpc_mqdec_t *dec, FILE *out);
181793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
182793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/******************************************************************************\
183793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* EVERYTHING BELOW THIS POINT IS IMPLEMENTATION SPECIFIC AND NOT PART OF THE
184793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* APPLICATION INTERFACE.  DO NOT RELY ON ANY OF THE INTERNAL FUNCTIONS/MACROS
185793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler* GIVEN BELOW.
186793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler\******************************************************************************/
187793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
188793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_getbit_macro(dec) \
189793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ((((dec)->areg -= (*(dec)->curctx)->qeval), \
190793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      (dec)->creg >> 16 >= (*(dec)->curctx)->qeval) ? \
191793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      ((((dec)->creg -= (*(dec)->curctx)->qeval << 16), \
192793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      (dec)->areg & 0x8000) ?  (*(dec)->curctx)->mps : \
193793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      jpc_mqdec_mpsexchrenormd(dec)) : \
194793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      jpc_mqdec_lpsexchrenormd(dec))
195793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
196793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_mpsexchange(areg, delta, curctx, bit) \
197793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ \
198793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if ((areg) < (delta)) { \
199793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        register jpc_mqstate_t *state = *(curctx); \
200793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        /* LPS decoded. */ \
201793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (bit) = state->mps ^ 1; \
202793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        *(curctx) = state->nlps; \
203793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } else { \
204793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        register jpc_mqstate_t *state = *(curctx); \
205793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        /* MPS decoded. */ \
206793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (bit) = state->mps; \
207793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        *(curctx) = state->nmps; \
208793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } \
209793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
210793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
211793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_lpsexchange(areg, delta, curctx, bit) \
212793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ \
213793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if ((areg) >= (delta)) { \
214793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        register jpc_mqstate_t *state = *(curctx); \
215793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (areg) = (delta); \
216793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (bit) = state->mps ^ 1; \
217793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        *(curctx) = state->nlps; \
218793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } else { \
219793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        register jpc_mqstate_t *state = *(curctx); \
220793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (areg) = (delta); \
221793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (bit) = state->mps; \
222793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        *(curctx) = state->nmps; \
223793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } \
224793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
225793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
226793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_renormd(areg, creg, ctreg, in, eof, inbuf) \
227793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ \
228793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    do { \
229793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        if (!(ctreg)) { \
230793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            jpc_mqdec_bytein2(creg, ctreg, in, eof, inbuf); \
231793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        } \
232793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (areg) <<= 1; \
233793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (creg) <<= 1; \
234793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        --(ctreg); \
235793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } while (!((areg) & 0x8000)); \
236793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
237793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
238793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define	jpc_mqdec_bytein2(creg, ctreg, in, eof, inbuf) \
239793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ \
240793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int c; \
241793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    unsigned char prevbuf; \
242793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if (!(eof)) { \
243793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        if ((c = jas_stream_getc(in)) == EOF) { \
244793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            (eof) = 1; \
245793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            c = 0xff; \
246793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        } \
247793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        prevbuf = (inbuf); \
248793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (inbuf) = c; \
249793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        if (prevbuf == 0xff) { \
250793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            if (c > 0x8f) { \
251793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                (creg) += 0xff00; \
252793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                (ctreg) = 8; \
253793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            } else { \
254793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                (creg) += c << 9; \
255793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                (ctreg) = 7; \
256793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            } \
257793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        } else { \
258793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            (creg) += c << 8; \
259793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            (ctreg) = 8; \
260793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        } \
261793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } else { \
262793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (creg) += 0xff00; \
263793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        (ctreg) = 8; \
264793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    } \
265793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
266793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
267793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint jpc_mqdec_getbit_func(jpc_mqdec_t *dec);
268793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint jpc_mqdec_mpsexchrenormd(jpc_mqdec_t *dec);
269793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint jpc_mqdec_lpsexchrenormd(jpc_mqdec_t *dec);
270793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
271793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
272