1 /* 2 * Copyright (c) 1999-2000, Image Power, Inc. and the University of 3 * British Columbia. 4 * Copyright (c) 2001-2003 Michael David Adams. 5 * All rights reserved. 6 */ 7 8 /* __START_OF_JASPER_LICENSE__ 9 * 10 * JasPer License Version 2.0 11 * 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 15 * 16 * All rights reserved. 17 * 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: 25 * 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. 29 * 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 32 * written permission. 33 * 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. 60 * 61 * __END_OF_JASPER_LICENSE__ 62 */ 63 64 /* 65 * Bit Stream Class 66 * 67 * $Id: jpc_bs.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $ 68 */ 69 70 /******************************************************************************\ 71 * Includes. 72 \******************************************************************************/ 73 74 #include <assert.h> 75 #include <stdlib.h> 76 #include <stdarg.h> 77 78 #include "jasper/jas_malloc.h" 79 #include "jasper/jas_math.h" 80 #include "jasper/jas_debug.h" 81 82 #include "jpc_bs.h" 83 84 /******************************************************************************\ 85 * Local function prototypes. 86 \******************************************************************************/ 87 88 static jpc_bitstream_t *jpc_bitstream_alloc(void); 89 90 /******************************************************************************\ 91 * Code for opening and closing bit streams. 92 \******************************************************************************/ 93 94 /* Open a bit stream from a stream. */ 95 jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, char *mode) 96 { 97 jpc_bitstream_t *bitstream; 98 99 /* Ensure that the open mode is valid. */ 100 #if 1 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+")); 104 #endif 105 106 if (!(bitstream = jpc_bitstream_alloc())) { 107 return 0; 108 } 109 110 /* By default, do not close the underlying (character) stream, upon 111 the close of the bit stream. */ 112 bitstream->flags_ = JPC_BITSTREAM_NOCLOSE; 113 114 bitstream->stream_ = stream; 115 bitstream->openmode_ = (mode[0] == 'w') ? JPC_BITSTREAM_WRITE : 116 JPC_BITSTREAM_READ; 117 118 /* Mark the data buffer as empty. */ 119 bitstream->cnt_ = (bitstream->openmode_ == JPC_BITSTREAM_READ) ? 0 : 8; 120 bitstream->buf_ = 0; 121 122 return bitstream; 123 } 124 125 /* Close a bit stream. */ 126 int jpc_bitstream_close(jpc_bitstream_t *bitstream) 127 { 128 int ret = 0; 129 130 /* Align to the next byte boundary while considering the effects of 131 bit stuffing. */ 132 if (jpc_bitstream_align(bitstream)) { 133 ret = -1; 134 } 135 136 /* If necessary, close the underlying (character) stream. */ 137 if (!(bitstream->flags_ & JPC_BITSTREAM_NOCLOSE) && bitstream->stream_) { 138 if (jas_stream_close(bitstream->stream_)) { 139 ret = -1; 140 } 141 bitstream->stream_ = 0; 142 } 143 144 jas_free(bitstream); 145 return ret; 146 } 147 148 /* Allocate a new bit stream. */ 149 static jpc_bitstream_t *jpc_bitstream_alloc() 150 { 151 jpc_bitstream_t *bitstream; 152 153 /* Allocate memory for the new bit stream object. */ 154 if (!(bitstream = jas_malloc(sizeof(jpc_bitstream_t)))) { 155 return 0; 156 } 157 /* Initialize all of the data members. */ 158 bitstream->stream_ = 0; 159 bitstream->cnt_ = 0; 160 bitstream->flags_ = 0; 161 bitstream->openmode_ = 0; 162 163 return bitstream; 164 } 165 166 /******************************************************************************\ 167 * Code for reading/writing from/to bit streams. 168 \******************************************************************************/ 169 170 /* Get a bit from a bit stream. */ 171 int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream) 172 { 173 int ret; 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)); 177 return ret; 178 } 179 180 /* Put a bit to a bit stream. */ 181 int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int b) 182 { 183 int ret; 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)); 187 return ret; 188 } 189 190 /* Get one or more bits from a bit stream. */ 191 long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n) 192 { 193 long v; 194 int u; 195 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); 199 200 /* Get the number of bits requested from the specified bit stream. */ 201 v = 0; 202 while (--n >= 0) { 203 if ((u = jpc_bitstream_getbit(bitstream)) < 0) { 204 return -1; 205 } 206 v = (v << 1) | u; 207 } 208 return v; 209 } 210 211 /* Put one or more bits to a bit stream. */ 212 int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v) 213 { 214 int m; 215 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)))); 221 222 /* Put the desired number of bits to the specified bit stream. */ 223 m = n - 1; 224 while (--n >= 0) { 225 if (jpc_bitstream_putbit(bitstream, (v >> m) & 1) == EOF) { 226 return EOF; 227 } 228 v <<= 1; 229 } 230 return 0; 231 } 232 233 /******************************************************************************\ 234 * Code for buffer filling and flushing. 235 \******************************************************************************/ 236 237 /* Fill the buffer for a bit stream. */ 238 int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream) 239 { 240 int c; 241 /* Note: The count has already been decremented by the caller. */ 242 assert(bitstream->openmode_ & JPC_BITSTREAM_READ); 243 assert(bitstream->cnt_ <= 0); 244 245 if (bitstream->flags_ & JPC_BITSTREAM_ERR) { 246 bitstream->cnt_ = 0; 247 return -1; 248 } 249 250 if (bitstream->flags_ & JPC_BITSTREAM_EOF) { 251 bitstream->buf_ = 0x7f; 252 bitstream->cnt_ = 7; 253 return 1; 254 } 255 256 bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff; 257 if ((c = jas_stream_getc((bitstream)->stream_)) == EOF) { 258 bitstream->flags_ |= JPC_BITSTREAM_EOF; 259 return 1; 260 } 261 bitstream->cnt_ = (bitstream->buf_ == 0xff00) ? 6 : 7; 262 bitstream->buf_ |= c & ((1 << (bitstream->cnt_ + 1)) - 1); 263 return (bitstream->buf_ >> bitstream->cnt_) & 1; 264 } 265 266 267 /******************************************************************************\ 268 * Code related to flushing. 269 \******************************************************************************/ 270 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) 274 { 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 279 required. */ 280 if ((bitstream->cnt_ < 8 && bitstream->cnt_ > 0) || 281 ((bitstream->buf_ >> 8) & 0xff) == 0xff) { 282 return 1; 283 } 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 288 required. */ 289 if ((bitstream->cnt_ < 8 && bitstream->cnt_ >= 0) || 290 ((bitstream->buf_ >> 8) & 0xff) == 0xff) { 291 return 1; 292 } 293 } else { 294 /* This should not happen. Famous last words, eh? :-) */ 295 assert(0); 296 return -1; 297 } 298 return 0; 299 } 300 301 /* How many additional bytes would be output if we align the bit stream? */ 302 int jpc_bitstream_pending(jpc_bitstream_t *bitstream) 303 { 304 if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) { 305 /* The bit stream is being used for writing. */ 306 #if 1 307 /* XXX - Is this really correct? Check someday... */ 308 if (bitstream->cnt_ < 8) { 309 return 1; 310 } 311 #else 312 if (bitstream->cnt_ < 8) { 313 if (((bitstream->buf_ >> 8) & 0xff) == 0xff) { 314 return 2; 315 } 316 return 1; 317 } 318 #endif 319 return 0; 320 } else { 321 /* This operation should not be invoked on a bit stream that 322 is being used for reading. */ 323 return -1; 324 } 325 } 326 327 /* Align the bit stream to a byte boundary. */ 328 int jpc_bitstream_align(jpc_bitstream_t *bitstream) 329 { 330 int ret; 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); 335 } else { 336 abort(); 337 } 338 return ret; 339 } 340 341 /* Align a bit stream in the input case. */ 342 int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask, 343 int filldata) 344 { 345 int n; 346 int v; 347 int u; 348 int numfill; 349 int m; 350 351 numfill = 7; 352 m = 0; 353 v = 0; 354 if (bitstream->cnt_ > 0) { 355 n = bitstream->cnt_; 356 } else if (!bitstream->cnt_) { 357 n = ((bitstream->buf_ & 0xff) == 0xff) ? 7 : 0; 358 } else { 359 n = 0; 360 } 361 if (n > 0) { 362 if ((u = jpc_bitstream_getbits(bitstream, n)) < 0) { 363 return -1; 364 } 365 m += n; 366 v = (v << n) | u; 367 } 368 if ((bitstream->buf_ & 0xff) == 0xff) { 369 if ((u = jpc_bitstream_getbits(bitstream, 7)) < 0) { 370 return -1; 371 } 372 v = (v << 7) | u; 373 m += 7; 374 } 375 if (m > numfill) { 376 v >>= m - numfill; 377 } else { 378 filldata >>= numfill - m; 379 fillmask >>= numfill - m; 380 } 381 if (((~(v ^ filldata)) & fillmask) != fillmask) { 382 /* The actual fill pattern does not match the expected one. */ 383 return 1; 384 } 385 386 return 0; 387 } 388 389 /* Align a bit stream in the output case. */ 390 int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata) 391 { 392 int n; 393 int v; 394 395 /* Ensure that this bit stream is open for writing. */ 396 assert(bitstream->openmode_ & JPC_BITSTREAM_WRITE); 397 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))); 403 404 if (!bitstream->cnt_) { 405 if ((bitstream->buf_ & 0xff) == 0xff) { 406 n = 7; 407 v = filldata; 408 } else { 409 n = 0; 410 v = 0; 411 } 412 } else if (bitstream->cnt_ > 0 && bitstream->cnt_ < 8) { 413 n = bitstream->cnt_; 414 v = filldata >> (7 - n); 415 } else { 416 n = 0; 417 v = 0; 418 return 0; 419 } 420 421 /* Write the appropriate fill data to the bit stream. */ 422 if (n > 0) { 423 if (jpc_bitstream_putbits(bitstream, n, v)) { 424 return -1; 425 } 426 } 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) { 433 return -1; 434 } 435 bitstream->cnt_ = 8; 436 bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff; 437 } 438 439 return 0; 440 } 441