/*1* Copyright (c) 1999-2000 Image Power, Inc. and the University of2* British Columbia.3* Copyright (c) 2001-2002 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 Encoder65*66* $Id: jpc_mqenc.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $67*/6869#ifndef JPC_MQENC_H70#define JPC_MQENC_H7172/******************************************************************************\73* Includes.74\******************************************************************************/7576#include "jasper/jas_types.h"77#include "jasper/jas_stream.h"7879#include "jpc_mqcod.h"8081/******************************************************************************\82* Constants.83\******************************************************************************/8485/*86* Termination modes.87*/8889#define JPC_MQENC_DEFTERM 0 /* default termination */90#define JPC_MQENC_PTERM 1 /* predictable termination */9192/******************************************************************************\93* Types.94\******************************************************************************/9596/* MQ arithmetic encoder class. */9798typedef struct {99100/* The C register. */101uint_fast32_t creg;102103/* The A register. */104uint_fast32_t areg;105106/* The CT register. */107uint_fast32_t ctreg;108109/* The maximum number of contexts. */110int maxctxs;111112/* The per-context information. */113jpc_mqstate_t **ctxs;114115/* The current context. */116jpc_mqstate_t **curctx;117118/* The stream for encoder output. */119jas_stream_t *out;120121/* The byte buffer (i.e., the B variable in the standard). */122int_fast16_t outbuf;123124/* The last byte output. */125int_fast16_t lastbyte;126127/* The error indicator. */128int err;129130} jpc_mqenc_t;131132/* MQ arithmetic encoder state information. */133134typedef struct {135136/* The A register. */137unsigned areg;138139/* The C register. */140unsigned creg;141142/* The CT register. */143unsigned ctreg;144145/* The last byte output by the encoder. */146int lastbyte;147148} jpc_mqencstate_t;149150/******************************************************************************\151* Functions/macros for construction and destruction.152\******************************************************************************/153154/* Create a MQ encoder. */155jpc_mqenc_t *jpc_mqenc_create(int maxctxs, jas_stream_t *out);156157/* Destroy a MQ encoder. */158void jpc_mqenc_destroy(jpc_mqenc_t *enc);159160/******************************************************************************\161* Functions/macros for initialization.162\******************************************************************************/163164/* Initialize a MQ encoder. */165void jpc_mqenc_init(jpc_mqenc_t *enc);166167/******************************************************************************\168* Functions/macros for context manipulation.169\******************************************************************************/170171/* Set the current context. */172#define jpc_mqenc_setcurctx(enc, ctxno) \173((enc)->curctx = &(enc)->ctxs[ctxno]);174175/* Set the state information for a particular context. */176void jpc_mqenc_setctx(jpc_mqenc_t *enc, int ctxno, jpc_mqctx_t *ctx);177178/* Set the state information for multiple contexts. */179void jpc_mqenc_setctxs(jpc_mqenc_t *enc, int numctxs, jpc_mqctx_t *ctxs);180181/******************************************************************************\182* Miscellaneous functions/macros.183\******************************************************************************/184185/* Get the error state of a MQ encoder. */186#define jpc_mqenc_error(enc) \187((enc)->err)188189/* Get the current encoder state. */190void jpc_mqenc_getstate(jpc_mqenc_t *enc, jpc_mqencstate_t *state);191192/* Terminate the code. */193int jpc_mqenc_flush(jpc_mqenc_t *enc, int termmode);194195/******************************************************************************\196* Functions/macros for encoding bits.197\******************************************************************************/198199/* Encode a bit. */200#if !defined(DEBUG)201#define jpc_mqenc_putbit(enc, bit) jpc_mqenc_putbit_macro(enc, bit)202#else203#define jpc_mqenc_putbit(enc, bit) jpc_mqenc_putbit_func(enc, bit)204#endif205206/******************************************************************************\207* Functions/macros for debugging.208\******************************************************************************/209210int jpc_mqenc_dump(jpc_mqenc_t *mqenc, FILE *out);211212/******************************************************************************\213* Implementation-specific details.214\******************************************************************************/215216/* Note: This macro is included only to satisfy the needs of217the mqenc_putbit macro. */218#define jpc_mqenc_putbit_macro(enc, bit) \219(((*((enc)->curctx))->mps == (bit)) ? \220(((enc)->areg -= (*(enc)->curctx)->qeval), \221((!((enc)->areg & 0x8000)) ? (jpc_mqenc_codemps2(enc)) : \222((enc)->creg += (*(enc)->curctx)->qeval))) : \223jpc_mqenc_codelps(enc))224225/* Note: These function prototypes are included only to satisfy the226needs of the mqenc_putbit_macro macro. Do not call any of these227functions directly. */228int jpc_mqenc_codemps2(jpc_mqenc_t *enc);229int jpc_mqenc_codelps(jpc_mqenc_t *enc);230231/* Note: This function prototype is included only to satisfy the needs of232the mqenc_putbit macro. */233int jpc_mqenc_putbit_func(jpc_mqenc_t *enc, int bit);234235#endif236237238