/*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 Encoder65*66* $Id: jpc_mqenc.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $67*/6869/******************************************************************************\70* Includes.71\******************************************************************************/7273#include <assert.h>74#include <stdlib.h>7576#include "jasper/jas_stream.h"77#include "jasper/jas_malloc.h"78#include "jasper/jas_math.h"79#include "jasper/jas_debug.h"8081#include "jpc_mqenc.h"8283/******************************************************************************\84* Macros85\******************************************************************************/8687#if defined(DEBUG)88#define JPC_MQENC_CALL(n, x) \89((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0))90#else91#define JPC_MQENC_CALL(n, x)92#endif9394#define jpc_mqenc_codemps9(areg, creg, ctreg, curctx, enc) \95{ \96jpc_mqstate_t *state = *(curctx); \97(areg) -= state->qeval; \98if (!((areg) & 0x8000)) { \99if ((areg) < state->qeval) { \100(areg) = state->qeval; \101} else { \102(creg) += state->qeval; \103} \104*(curctx) = state->nmps; \105jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \106} else { \107(creg) += state->qeval; \108} \109}110111#define jpc_mqenc_codelps2(areg, creg, ctreg, curctx, enc) \112{ \113jpc_mqstate_t *state = *(curctx); \114(areg) -= state->qeval; \115if ((areg) < state->qeval) { \116(creg) += state->qeval; \117} else { \118(areg) = state->qeval; \119} \120*(curctx) = state->nlps; \121jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \122}123124#define jpc_mqenc_renorme(areg, creg, ctreg, enc) \125{ \126do { \127(areg) <<= 1; \128(creg) <<= 1; \129if (!--(ctreg)) { \130jpc_mqenc_byteout((areg), (creg), (ctreg), (enc)); \131} \132} while (!((areg) & 0x8000)); \133}134135#define jpc_mqenc_byteout(areg, creg, ctreg, enc) \136{ \137if ((enc)->outbuf != 0xff) { \138if ((creg) & 0x8000000) { \139if (++((enc)->outbuf) == 0xff) { \140(creg) &= 0x7ffffff; \141jpc_mqenc_byteout2(enc); \142enc->outbuf = ((creg) >> 20) & 0xff; \143(creg) &= 0xfffff; \144(ctreg) = 7; \145} else { \146jpc_mqenc_byteout2(enc); \147enc->outbuf = ((creg) >> 19) & 0xff; \148(creg) &= 0x7ffff; \149(ctreg) = 8; \150} \151} else { \152jpc_mqenc_byteout2(enc); \153(enc)->outbuf = ((creg) >> 19) & 0xff; \154(creg) &= 0x7ffff; \155(ctreg) = 8; \156} \157} else { \158jpc_mqenc_byteout2(enc); \159(enc)->outbuf = ((creg) >> 20) & 0xff; \160(creg) &= 0xfffff; \161(ctreg) = 7; \162} \163}164165#define jpc_mqenc_byteout2(enc) \166{ \167if (enc->outbuf >= 0) { \168if (jas_stream_putc(enc->out, (unsigned char)enc->outbuf) == EOF) { \169enc->err |= 1; \170} \171} \172enc->lastbyte = enc->outbuf; \173}174175/******************************************************************************\176* Local function protoypes.177\******************************************************************************/178179static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc);180181/******************************************************************************\182* Code for creation and destruction of encoder.183\******************************************************************************/184185/* Create a MQ encoder. */186187jpc_mqenc_t *jpc_mqenc_create(int maxctxs, jas_stream_t *out)188{189jpc_mqenc_t *mqenc;190191/* Allocate memory for the MQ encoder. */192if (!(mqenc = jas_malloc(sizeof(jpc_mqenc_t)))) {193goto error;194}195mqenc->out = out;196mqenc->maxctxs = maxctxs;197198/* Allocate memory for the per-context state information. */199if (!(mqenc->ctxs = jas_alloc2(mqenc->maxctxs, sizeof(jpc_mqstate_t *)))) {200goto error;201}202203/* Set the current context to the first one. */204mqenc->curctx = mqenc->ctxs;205206jpc_mqenc_init(mqenc);207208/* Initialize the per-context state information to something sane. */209jpc_mqenc_setctxs(mqenc, 0, 0);210211return mqenc;212213error:214if (mqenc) {215jpc_mqenc_destroy(mqenc);216}217return 0;218}219220/* Destroy a MQ encoder. */221222void jpc_mqenc_destroy(jpc_mqenc_t *mqenc)223{224if (mqenc->ctxs) {225jas_free(mqenc->ctxs);226}227jas_free(mqenc);228}229230/******************************************************************************\231* State initialization code.232\******************************************************************************/233234/* Initialize the coding state of a MQ encoder. */235236void jpc_mqenc_init(jpc_mqenc_t *mqenc)237{238mqenc->areg = 0x8000;239mqenc->outbuf = -1;240mqenc->creg = 0;241mqenc->ctreg = 12;242mqenc->lastbyte = -1;243mqenc->err = 0;244}245246/* Initialize one or more contexts. */247248void jpc_mqenc_setctxs(jpc_mqenc_t *mqenc, int numctxs, jpc_mqctx_t *ctxs)249{250jpc_mqstate_t **ctx;251int n;252253ctx = mqenc->ctxs;254n = JAS_MIN(mqenc->maxctxs, numctxs);255while (--n >= 0) {256*ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps];257++ctx;258++ctxs;259}260n = mqenc->maxctxs - numctxs;261while (--n >= 0) {262*ctx = &jpc_mqstates[0];263++ctx;264}265266}267268/* Get the coding state for a MQ encoder. */269270void jpc_mqenc_getstate(jpc_mqenc_t *mqenc, jpc_mqencstate_t *state)271{272state->areg = mqenc->areg;273state->creg = mqenc->creg;274state->ctreg = mqenc->ctreg;275state->lastbyte = mqenc->lastbyte;276}277278/******************************************************************************\279* Code for coding symbols.280\******************************************************************************/281282/* Encode a bit. */283284int jpc_mqenc_putbit_func(jpc_mqenc_t *mqenc, int bit)285{286const jpc_mqstate_t *state;287JAS_DBGLOG(100, ("jpc_mqenc_putbit(%p, %d)\n", mqenc, bit));288JPC_MQENC_CALL(100, jpc_mqenc_dump(mqenc, stderr));289290state = *(mqenc->curctx);291292if (state->mps == bit) {293/* Apply the CODEMPS algorithm as defined in the standard. */294mqenc->areg -= state->qeval;295if (!(mqenc->areg & 0x8000)) {296jpc_mqenc_codemps2(mqenc);297} else {298mqenc->creg += state->qeval;299}300} else {301/* Apply the CODELPS algorithm as defined in the standard. */302jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc);303}304305return jpc_mqenc_error(mqenc) ? (-1) : 0;306}307308int jpc_mqenc_codemps2(jpc_mqenc_t *mqenc)309{310/* Note: This function only performs part of the work associated with311the CODEMPS algorithm from the standard. Some of the work is also312performed by the caller. */313314jpc_mqstate_t *state = *(mqenc->curctx);315if (mqenc->areg < state->qeval) {316mqenc->areg = state->qeval;317} else {318mqenc->creg += state->qeval;319}320*mqenc->curctx = state->nmps;321jpc_mqenc_renorme(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);322return jpc_mqenc_error(mqenc) ? (-1) : 0;323}324325int jpc_mqenc_codelps(jpc_mqenc_t *mqenc)326{327jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc);328return jpc_mqenc_error(mqenc) ? (-1) : 0;329}330331/******************************************************************************\332* Miscellaneous code.333\******************************************************************************/334335/* Terminate the code word. */336337int jpc_mqenc_flush(jpc_mqenc_t *mqenc, int termmode)338{339int_fast16_t k;340341switch (termmode) {342case JPC_MQENC_PTERM:343k = 11 - mqenc->ctreg + 1;344while (k > 0) {345mqenc->creg <<= mqenc->ctreg;346mqenc->ctreg = 0;347jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg,348mqenc);349k -= mqenc->ctreg;350}351if (mqenc->outbuf != 0xff) {352jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);353}354break;355case JPC_MQENC_DEFTERM:356jpc_mqenc_setbits(mqenc);357mqenc->creg <<= mqenc->ctreg;358jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);359mqenc->creg <<= mqenc->ctreg;360jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);361if (mqenc->outbuf != 0xff) {362jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);363}364break;365default:366abort();367break;368}369return 0;370}371372static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc)373{374uint_fast32_t tmp = mqenc->creg + mqenc->areg;375mqenc->creg |= 0xffff;376if (mqenc->creg >= tmp) {377mqenc->creg -= 0x8000;378}379}380381/* Dump a MQ encoder to a stream for debugging. */382383int jpc_mqenc_dump(jpc_mqenc_t *mqenc, FILE *out)384{385fprintf(out, "AREG = %08x, CREG = %08x, CTREG = %d\n",386(unsigned)mqenc->areg, (unsigned)mqenc->creg, (int)mqenc->ctreg);387fprintf(out, "IND = %02d, MPS = %d, QEVAL = %04x\n",388(int)(*mqenc->curctx - jpc_mqstates), (int)(*mqenc->curctx)->mps,389(int)(*mqenc->curctx)->qeval);390return 0;391}392393394