2 * Copyright (c) 1999-2000, Image Power, Inc. and the University of
4 * Copyright (c) 2001-2003 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
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
16 * All rights reserved.
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:
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.
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
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.
61 * __END_OF_JASPER_LICENSE__
67 * $Id: jpc_bs.c,v 1.2 2008/05/26 09:40:52 vp153 Exp $
70 /******************************************************************************\
72 \******************************************************************************/
78 #include "jasper/jas_malloc.h"
79 #include "jasper/jas_math.h"
80 #include "jasper/jas_debug.h"
84 /******************************************************************************\
85 * Local function prototypes.
86 \******************************************************************************/
88 static jpc_bitstream_t *jpc_bitstream_alloc(void);
90 /******************************************************************************\
91 * Code for opening and closing bit streams.
92 \******************************************************************************/
94 /* Open a bit stream from a stream. */
95 jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, char *mode)
97 jpc_bitstream_t *bitstream;
99 /* Ensure that the open mode is valid. */
101 /* This causes a string literal too long error (with c99 pedantic mode). */
102 assert(!strcmp(mode, "r") || !strcmp(mode, "w") || !strcmp(mode, "r+")
103 || !strcmp(mode, "w+"));
106 if (!(bitstream = jpc_bitstream_alloc())) {
110 /* By default, do not close the underlying (character) stream, upon
111 the close of the bit stream. */
112 bitstream->flags_ = JPC_BITSTREAM_NOCLOSE;
114 bitstream->stream_ = stream;
115 bitstream->openmode_ = (mode[0] == 'w') ? JPC_BITSTREAM_WRITE :
118 /* Mark the data buffer as empty. */
119 bitstream->cnt_ = (bitstream->openmode_ == JPC_BITSTREAM_READ) ? 0 : 8;
125 /* Close a bit stream. */
126 int jpc_bitstream_close(jpc_bitstream_t *bitstream)
130 /* Align to the next byte boundary while considering the effects of
132 if (jpc_bitstream_align(bitstream)) {
136 /* If necessary, close the underlying (character) stream. */
137 if (!(bitstream->flags_ & JPC_BITSTREAM_NOCLOSE) && bitstream->stream_) {
138 if (jas_stream_close(bitstream->stream_)) {
141 bitstream->stream_ = 0;
148 /* Allocate a new bit stream. */
149 static jpc_bitstream_t *jpc_bitstream_alloc()
151 jpc_bitstream_t *bitstream;
153 /* Allocate memory for the new bit stream object. */
154 if (!(bitstream = jas_malloc(sizeof(jpc_bitstream_t)))) {
157 /* Initialize all of the data members. */
158 bitstream->stream_ = 0;
160 bitstream->flags_ = 0;
161 bitstream->openmode_ = 0;
166 /******************************************************************************\
167 * Code for reading/writing from/to bit streams.
168 \******************************************************************************/
170 /* Get a bit from a bit stream. */
171 int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream)
174 JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func(%p)\n", bitstream));
175 ret = jpc_bitstream_getbit_macro(bitstream);
176 JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func -> %d\n", ret));
180 /* Put a bit to a bit stream. */
181 int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int b)
184 JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func(%p, %d)\n", bitstream, b));
185 ret = jpc_bitstream_putbit_macro(bitstream, b);
186 JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func() -> %d\n", ret));
190 /* Get one or more bits from a bit stream. */
191 long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n)
196 /* We can reliably get at most 31 bits since ISO/IEC 9899 only
197 guarantees that a long can represent values up to 2^31-1. */
198 assert(n >= 0 && n < 32);
200 /* Get the number of bits requested from the specified bit stream. */
203 if ((u = jpc_bitstream_getbit(bitstream)) < 0) {
211 /* Put one or more bits to a bit stream. */
212 int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v)
216 /* We can reliably put at most 31 bits since ISO/IEC 9899 only
217 guarantees that a long can represent values up to 2^31-1. */
218 assert(n >= 0 && n < 32);
219 /* Ensure that only the bits to be output are nonzero. */
220 assert(!(v & (~JAS_ONES(n))));
222 /* Put the desired number of bits to the specified bit stream. */
225 if (jpc_bitstream_putbit(bitstream, (v >> m) & 1) == EOF) {
233 /******************************************************************************\
234 * Code for buffer filling and flushing.
235 \******************************************************************************/
237 /* Fill the buffer for a bit stream. */
238 int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream)
241 /* Note: The count has already been decremented by the caller. */
242 assert(bitstream->openmode_ & JPC_BITSTREAM_READ);
243 assert(bitstream->cnt_ <= 0);
245 if (bitstream->flags_ & JPC_BITSTREAM_ERR) {
250 if (bitstream->flags_ & JPC_BITSTREAM_EOF) {
251 bitstream->buf_ = 0x7f;
256 bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff;
257 if ((c = jas_stream_getc((bitstream)->stream_)) == EOF) {
258 bitstream->flags_ |= JPC_BITSTREAM_EOF;
261 bitstream->cnt_ = (bitstream->buf_ == 0xff00) ? 6 : 7;
262 bitstream->buf_ |= c & ((1 << (bitstream->cnt_ + 1)) - 1);
263 return (bitstream->buf_ >> bitstream->cnt_) & 1;
267 /******************************************************************************\
268 * Code related to flushing.
269 \******************************************************************************/
271 /* Does the bit stream need to be aligned to a byte boundary (considering
272 the effects of bit stuffing)? */
273 int jpc_bitstream_needalign(jpc_bitstream_t *bitstream)
275 if (bitstream->openmode_ & JPC_BITSTREAM_READ) {
276 /* The bit stream is open for reading. */
277 /* If there are any bits buffered for reading, or the
278 previous byte forced a stuffed bit, alignment is
280 if ((bitstream->cnt_ < 8 && bitstream->cnt_ > 0) ||
281 ((bitstream->buf_ >> 8) & 0xff) == 0xff) {
284 } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
285 /* The bit stream is open for writing. */
286 /* If there are any bits buffered for writing, or the
287 previous byte forced a stuffed bit, alignment is
289 if ((bitstream->cnt_ < 8 && bitstream->cnt_ >= 0) ||
290 ((bitstream->buf_ >> 8) & 0xff) == 0xff) {
294 /* This should not happen. Famous last words, eh? :-) */
301 /* How many additional bytes would be output if we align the bit stream? */
302 int jpc_bitstream_pending(jpc_bitstream_t *bitstream)
304 if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
305 /* The bit stream is being used for writing. */
307 /* XXX - Is this really correct? Check someday... */
308 if (bitstream->cnt_ < 8) {
312 if (bitstream->cnt_ < 8) {
313 if (((bitstream->buf_ >> 8) & 0xff) == 0xff) {
321 /* This operation should not be invoked on a bit stream that
322 is being used for reading. */
327 /* Align the bit stream to a byte boundary. */
328 int jpc_bitstream_align(jpc_bitstream_t *bitstream)
331 if (bitstream->openmode_ & JPC_BITSTREAM_READ) {
332 ret = jpc_bitstream_inalign(bitstream, 0, 0);
333 } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
334 ret = jpc_bitstream_outalign(bitstream, 0);
341 /* Align a bit stream in the input case. */
342 int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask,
354 if (bitstream->cnt_ > 0) {
356 } else if (!bitstream->cnt_) {
357 n = ((bitstream->buf_ & 0xff) == 0xff) ? 7 : 0;
362 if ((u = jpc_bitstream_getbits(bitstream, n)) < 0) {
368 if ((bitstream->buf_ & 0xff) == 0xff) {
369 if ((u = jpc_bitstream_getbits(bitstream, 7)) < 0) {
378 filldata >>= numfill - m;
379 fillmask >>= numfill - m;
381 if (((~(v ^ filldata)) & fillmask) != fillmask) {
382 /* The actual fill pattern does not match the expected one. */
389 /* Align a bit stream in the output case. */
390 int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata)
395 /* Ensure that this bit stream is open for writing. */
396 assert(bitstream->openmode_ & JPC_BITSTREAM_WRITE);
398 /* Ensure that the first bit of fill data is zero. */
399 /* Note: The first bit of fill data must be zero. If this were not
400 the case, the fill data itself could cause further bit stuffing to
401 be required (which would cause numerous complications). */
402 assert(!(filldata & (~0x3f)));
404 if (!bitstream->cnt_) {
405 if ((bitstream->buf_ & 0xff) == 0xff) {
412 } else if (bitstream->cnt_ > 0 && bitstream->cnt_ < 8) {
414 v = filldata >> (7 - n);
421 /* Write the appropriate fill data to the bit stream. */
423 if (jpc_bitstream_putbits(bitstream, n, v)) {
427 if (bitstream->cnt_ < 8) {
428 assert(bitstream->cnt_ >= 0 && bitstream->cnt_ < 8);
429 assert((bitstream->buf_ & 0xff) != 0xff);
430 /* Force the pending byte of output to be written to the
431 underlying (character) stream. */
432 if (jas_stream_putc(bitstream->stream_, bitstream->buf_ & 0xff) == EOF) {
436 bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff;