/*1* Copyright (c) 1999-2000 Image Power, Inc. and the University of2* British Columbia.3* Copyright (c) 2001-2003 Michael David Adams.4* All rights reserved.5*/67/* __START_OF_JASPER_LICENSE__8*9* JasPer License Version 2.010*11* Copyright (c) 2001-2006 Michael David Adams12* Copyright (c) 1999-2000 Image Power, Inc.13* Copyright (c) 1999-2000 The University of British Columbia14*15* All rights reserved.16*17* Permission is hereby granted, free of charge, to any person (the18* "User") obtaining a copy of this software and associated documentation19* files (the "Software"), to deal in the Software without restriction,20* including without limitation the rights to use, copy, modify, merge,21* publish, distribute, and/or sell copies of the Software, and to permit22* persons to whom the Software is furnished to do so, subject to the23* following conditions:24*25* 1. The above copyright notices and this permission notice (which26* includes the disclaimer below) shall be included in all copies or27* substantial portions of the Software.28*29* 2. The name of a copyright holder shall not be used to endorse or30* promote products derived from the Software without specific prior31* written permission.32*33* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS34* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER35* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS36* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING37* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A38* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO39* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL40* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING41* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,42* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION43* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE44* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE45* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.46* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS47* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL48* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS49* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE50* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE51* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL52* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,53* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL54* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH55* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,56* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH57* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY58* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.59*60* __END_OF_JASPER_LICENSE__61*/6263/*64* MQ Arithmetic Decoder65*66* $Id: jpc_mqdec.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $67*/6869#ifndef JPC_MQDEC_H70#define JPC_MQDEC_H7172/******************************************************************************\73* Includes.74\******************************************************************************/7576#include "jasper/jas_types.h"77#include "jasper/jas_stream.h"7879#include "jpc_mqcod.h"8081/******************************************************************************\82* Types.83\******************************************************************************/8485/* MQ arithmetic decoder. */8687typedef struct {8889/* The C register. */90uint_fast32_t creg;9192/* The A register. */93uint_fast32_t areg;9495/* The CT register. */96uint_fast32_t ctreg;9798/* The current context. */99jpc_mqstate_t **curctx;100101/* The per-context information. */102jpc_mqstate_t **ctxs;103104/* The maximum number of contexts. */105int maxctxs;106107/* The stream from which to read data. */108jas_stream_t *in;109110/* The last character read. */111uchar inbuffer;112113/* The EOF indicator. */114int eof;115116} jpc_mqdec_t;117118/******************************************************************************\119* Functions/macros for construction and destruction.120\******************************************************************************/121122/* Create a MQ decoder. */123jpc_mqdec_t *jpc_mqdec_create(int maxctxs, jas_stream_t *in);124125/* Destroy a MQ decoder. */126void jpc_mqdec_destroy(jpc_mqdec_t *dec);127128/******************************************************************************\129* Functions/macros for initialization.130\******************************************************************************/131132/* Set the input stream associated with a MQ decoder. */133void jpc_mqdec_setinput(jpc_mqdec_t *dec, jas_stream_t *in);134135/* Initialize a MQ decoder. */136void jpc_mqdec_init(jpc_mqdec_t *dec);137138/******************************************************************************\139* Functions/macros for manipulating contexts.140\******************************************************************************/141142/* Set the current context for a MQ decoder. */143#define jpc_mqdec_setcurctx(dec, ctxno) \144((mqdec)->curctx = &(mqdec)->ctxs[ctxno]);145146/* Set the state information for a particular context of a MQ decoder. */147void jpc_mqdec_setctx(jpc_mqdec_t *dec, int ctxno, jpc_mqctx_t *ctx);148149/* Set the state information for all contexts of a MQ decoder. */150void jpc_mqdec_setctxs(jpc_mqdec_t *dec, int numctxs, jpc_mqctx_t *ctxs);151152/******************************************************************************\153* Functions/macros for decoding bits.154\******************************************************************************/155156/* Decode a symbol. */157#if !defined(DEBUG)158#define jpc_mqdec_getbit(dec) \159jpc_mqdec_getbit_macro(dec)160#else161#define jpc_mqdec_getbit(dec) \162jpc_mqdec_getbit_func(dec)163#endif164165/* Decode a symbol (assuming an unskewed probability distribution). */166#if !defined(DEBUG)167#define jpc_mqdec_getbitnoskew(dec) \168jpc_mqdec_getbit_macro(dec)169#else170#define jpc_mqdec_getbitnoskew(dec) \171jpc_mqdec_getbit_func(dec)172#endif173174/******************************************************************************\175* Functions/macros for debugging.176\******************************************************************************/177178/* Dump the MQ decoder state for debugging. */179void jpc_mqdec_dump(jpc_mqdec_t *dec, FILE *out);180181/******************************************************************************\182* EVERYTHING BELOW THIS POINT IS IMPLEMENTATION SPECIFIC AND NOT PART OF THE183* APPLICATION INTERFACE. DO NOT RELY ON ANY OF THE INTERNAL FUNCTIONS/MACROS184* GIVEN BELOW.185\******************************************************************************/186187#define jpc_mqdec_getbit_macro(dec) \188((((dec)->areg -= (*(dec)->curctx)->qeval), \189(dec)->creg >> 16 >= (*(dec)->curctx)->qeval) ? \190((((dec)->creg -= (*(dec)->curctx)->qeval << 16), \191(dec)->areg & 0x8000) ? (*(dec)->curctx)->mps : \192jpc_mqdec_mpsexchrenormd(dec)) : \193jpc_mqdec_lpsexchrenormd(dec))194195#define jpc_mqdec_mpsexchange(areg, delta, curctx, bit) \196{ \197if ((areg) < (delta)) { \198register jpc_mqstate_t *state = *(curctx); \199/* LPS decoded. */ \200(bit) = state->mps ^ 1; \201*(curctx) = state->nlps; \202} else { \203register jpc_mqstate_t *state = *(curctx); \204/* MPS decoded. */ \205(bit) = state->mps; \206*(curctx) = state->nmps; \207} \208}209210#define jpc_mqdec_lpsexchange(areg, delta, curctx, bit) \211{ \212if ((areg) >= (delta)) { \213register jpc_mqstate_t *state = *(curctx); \214(areg) = (delta); \215(bit) = state->mps ^ 1; \216*(curctx) = state->nlps; \217} else { \218register jpc_mqstate_t *state = *(curctx); \219(areg) = (delta); \220(bit) = state->mps; \221*(curctx) = state->nmps; \222} \223}224225#define jpc_mqdec_renormd(areg, creg, ctreg, in, eof, inbuf) \226{ \227do { \228if (!(ctreg)) { \229jpc_mqdec_bytein2(creg, ctreg, in, eof, inbuf); \230} \231(areg) <<= 1; \232(creg) <<= 1; \233--(ctreg); \234} while (!((areg) & 0x8000)); \235}236237#define jpc_mqdec_bytein2(creg, ctreg, in, eof, inbuf) \238{ \239int c; \240unsigned char prevbuf; \241if (!(eof)) { \242if ((c = jas_stream_getc(in)) == EOF) { \243(eof) = 1; \244c = 0xff; \245} \246prevbuf = (inbuf); \247(inbuf) = c; \248if (prevbuf == 0xff) { \249if (c > 0x8f) { \250(creg) += 0xff00; \251(ctreg) = 8; \252} else { \253(creg) += c << 9; \254(ctreg) = 7; \255} \256} else { \257(creg) += c << 8; \258(ctreg) = 8; \259} \260} else { \261(creg) += 0xff00; \262(ctreg) = 8; \263} \264}265266int jpc_mqdec_getbit_func(jpc_mqdec_t *dec);267int jpc_mqdec_mpsexchrenormd(jpc_mqdec_t *dec);268int jpc_mqdec_lpsexchrenormd(jpc_mqdec_t *dec);269270#endif271272273