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) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
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.1 2005/05/22 18:33:03 malaterre 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. */
100 assert(!strcmp(mode, "r") || !strcmp(mode, "w") || !strcmp(mode, "r+")
101 || !strcmp(mode, "w+"));
103 if (!(bitstream = jpc_bitstream_alloc())) {
107 /* By default, do not close the underlying (character) stream, upon
108 the close of the bit stream. */
109 bitstream->flags_ = JPC_BITSTREAM_NOCLOSE;
111 bitstream->stream_ = stream;
112 bitstream->openmode_ = (mode[0] == 'w') ? JPC_BITSTREAM_WRITE :
115 /* Mark the data buffer as empty. */
116 bitstream->cnt_ = (bitstream->openmode_ == JPC_BITSTREAM_READ) ? 0 : 8;
122 /* Close a bit stream. */
123 int jpc_bitstream_close(jpc_bitstream_t *bitstream)
127 /* Align to the next byte boundary while considering the effects of
129 if (jpc_bitstream_align(bitstream)) {
133 /* If necessary, close the underlying (character) stream. */
134 if (!(bitstream->flags_ & JPC_BITSTREAM_NOCLOSE) && bitstream->stream_) {
135 if (jas_stream_close(bitstream->stream_)) {
138 bitstream->stream_ = 0;
145 /* Allocate a new bit stream. */
146 static jpc_bitstream_t *jpc_bitstream_alloc()
148 jpc_bitstream_t *bitstream;
150 /* Allocate memory for the new bit stream object. */
151 if (!(bitstream = jas_malloc(sizeof(jpc_bitstream_t)))) {
154 /* Initialize all of the data members. */
155 bitstream->stream_ = 0;
157 bitstream->flags_ = 0;
158 bitstream->openmode_ = 0;
163 /******************************************************************************\
164 * Code for reading/writing from/to bit streams.
165 \******************************************************************************/
167 /* Get a bit from a bit stream. */
168 int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream)
171 JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func(%p)\n", bitstream));
172 ret = jpc_bitstream_getbit_macro(bitstream);
173 JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func -> %d\n", ret));
177 /* Put a bit to a bit stream. */
178 int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int b)
181 JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func(%p, %d)\n", bitstream, b));
182 ret = jpc_bitstream_putbit_macro(bitstream, b);
183 JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func() -> %d\n", ret));
187 /* Get one or more bits from a bit stream. */
188 long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n)
193 /* We can reliably get at most 31 bits since ISO/IEC 9899 only
194 guarantees that a long can represent values up to 2^31-1. */
195 assert(n >= 0 && n < 32);
197 /* Get the number of bits requested from the specified bit stream. */
200 if ((u = jpc_bitstream_getbit(bitstream)) < 0) {
208 /* Put one or more bits to a bit stream. */
209 int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v)
213 /* We can reliably put at most 31 bits since ISO/IEC 9899 only
214 guarantees that a long can represent values up to 2^31-1. */
215 assert(n >= 0 && n < 32);
216 /* Ensure that only the bits to be output are nonzero. */
217 assert(!(v & (~JAS_ONES(n))));
219 /* Put the desired number of bits to the specified bit stream. */
222 if (jpc_bitstream_putbit(bitstream, (v >> m) & 1) == EOF) {
230 /******************************************************************************\
231 * Code for buffer filling and flushing.
232 \******************************************************************************/
234 /* Fill the buffer for a bit stream. */
235 int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream)
238 /* Note: The count has already been decremented by the caller. */
239 assert(bitstream->openmode_ & JPC_BITSTREAM_READ);
240 assert(bitstream->cnt_ <= 0);
242 if (bitstream->flags_ & JPC_BITSTREAM_ERR) {
247 if (bitstream->flags_ & JPC_BITSTREAM_EOF) {
248 bitstream->buf_ = 0x7f;
253 bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff;
254 if ((c = jas_stream_getc((bitstream)->stream_)) == EOF) {
255 bitstream->flags_ |= JPC_BITSTREAM_EOF;
258 bitstream->cnt_ = (bitstream->buf_ == 0xff00) ? 6 : 7;
259 bitstream->buf_ |= c & ((1 << (bitstream->cnt_ + 1)) - 1);
260 return (bitstream->buf_ >> bitstream->cnt_) & 1;
264 /******************************************************************************\
265 * Code related to flushing.
266 \******************************************************************************/
268 /* Does the bit stream need to be aligned to a byte boundary (considering
269 the effects of bit stuffing)? */
270 int jpc_bitstream_needalign(jpc_bitstream_t *bitstream)
272 if (bitstream->openmode_ & JPC_BITSTREAM_READ) {
273 /* The bit stream is open for reading. */
274 /* If there are any bits buffered for reading, or the
275 previous byte forced a stuffed bit, alignment is
277 if ((bitstream->cnt_ < 8 && bitstream->cnt_ > 0) ||
278 ((bitstream->buf_ >> 8) & 0xff) == 0xff) {
281 } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
282 /* The bit stream is open for writing. */
283 /* If there are any bits buffered for writing, or the
284 previous byte forced a stuffed bit, alignment is
286 if ((bitstream->cnt_ < 8 && bitstream->cnt_ >= 0) ||
287 ((bitstream->buf_ >> 8) & 0xff) == 0xff) {
291 /* This should not happen. Famous last words, eh? :-) */
298 /* How many additional bytes would be output if we align the bit stream? */
299 int jpc_bitstream_pending(jpc_bitstream_t *bitstream)
301 if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
302 /* The bit stream is being used for writing. */
304 /* XXX - Is this really correct? Check someday... */
305 if (bitstream->cnt_ < 8) {
309 if (bitstream->cnt_ < 8) {
310 if (((bitstream->buf_ >> 8) & 0xff) == 0xff) {
318 /* This operation should not be invoked on a bit stream that
319 is being used for reading. */
324 /* Align the bit stream to a byte boundary. */
325 int jpc_bitstream_align(jpc_bitstream_t *bitstream)
328 if (bitstream->openmode_ & JPC_BITSTREAM_READ) {
329 ret = jpc_bitstream_inalign(bitstream, 0, 0);
330 } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
331 ret = jpc_bitstream_outalign(bitstream, 0);
338 /* Align a bit stream in the input case. */
339 int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask,
351 if (bitstream->cnt_ > 0) {
353 } else if (!bitstream->cnt_) {
354 n = ((bitstream->buf_ & 0xff) == 0xff) ? 7 : 0;
359 if ((u = jpc_bitstream_getbits(bitstream, n)) < 0) {
365 if ((bitstream->buf_ & 0xff) == 0xff) {
366 if ((u = jpc_bitstream_getbits(bitstream, 7)) < 0) {
375 filldata >>= numfill - m;
376 fillmask >>= numfill - m;
378 if (((~(v ^ filldata)) & fillmask) != fillmask) {
379 /* The actual fill pattern does not match the expected one. */
386 /* Align a bit stream in the output case. */
387 int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata)
392 /* Ensure that this bit stream is open for writing. */
393 assert(bitstream->openmode_ & JPC_BITSTREAM_WRITE);
395 /* Ensure that the first bit of fill data is zero. */
396 /* Note: The first bit of fill data must be zero. If this were not
397 the case, the fill data itself could cause further bit stuffing to
398 be required (which would cause numerous complications). */
399 assert(!(filldata & (~0x3f)));
401 if (!bitstream->cnt_) {
402 if ((bitstream->buf_ & 0xff) == 0xff) {
409 } else if (bitstream->cnt_ > 0 && bitstream->cnt_ < 8) {
411 v = filldata >> (7 - n);
418 /* Write the appropriate fill data to the bit stream. */
420 if (jpc_bitstream_putbits(bitstream, n, v)) {
424 if (bitstream->cnt_ < 8) {
425 assert(bitstream->cnt_ >= 0 && bitstream->cnt_ < 8);
426 assert((bitstream->buf_ & 0xff) != 0xff);
427 /* Force the pending byte of output to be written to the
428 underlying (character) stream. */
429 if (jas_stream_putc(bitstream->stream_, bitstream->buf_ & 0xff) == EOF) {
433 bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff;