Debian Codegraph @

converter/other/jpeg2000/libjasper/jpc/jpc_bs.c

raw
1 /*
2 * Copyright (c) 1999-2000, Image Power, Inc. and the University of
3 * British Columbia.
4 * Copyright (c) 2001-2002 Michael David Adams.
5 * All rights reserved.
6 */
7
8 /* __START_OF_JASPER_LICENSE__
9 *
10 * JasPer Software License
11 *
12 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13 * ************************************
14 *
15 * GRANT:
16 *
17 * Permission is hereby granted, free of charge, to any person (the "User")
18 * obtaining a copy of this software and associated documentation, to deal
19 * in the JasPer Software without restriction, including without limitation
20 * the right to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the JasPer Software (in source and binary forms),
22 * and to permit persons to whom the JasPer Software is furnished to do so,
23 * provided further that the License Conditions below are met.
24 *
25 * License Conditions
26 * ******************
27 *
28 * A. Redistributions of source code must retain the above copyright notice,
29 * and this list of conditions, and the following disclaimer.
30 *
31 * B. Redistributions in binary form must reproduce the above copyright
32 * notice, and this list of conditions, and the following disclaimer in
33 * the documentation and/or other materials provided with the distribution.
34 *
35 * C. Neither the name of Image Power, Inc. nor any other contributor
36 * (including, but not limited to, the University of British Columbia and
37 * Michael David Adams) may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
39 *
40 * D. User agrees that it shall not commence any action against Image Power,
41 * Inc., the University of British Columbia, Michael David Adams, or any
42 * other contributors (collectively "Licensors") for infringement of any
43 * intellectual property rights ("IPR") held by the User in respect of any
44 * technology that User owns or has a right to license or sublicense and
45 * which is an element required in order to claim compliance with ISO/IEC
46 * 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property
47 * rights worldwide arising under statutory or common law, and whether
48 * or not perfected, including, without limitation, all (i) patents and
49 * patent applications owned or licensable by User; (ii) rights associated
50 * with works of authorship including copyrights, copyright applications,
51 * copyright registrations, mask work rights, mask work applications,
52 * mask work registrations; (iii) rights relating to the protection of
53 * trade secrets and confidential information; (iv) any right analogous
54 * to those set forth in subsections (i), (ii), or (iii) and any other
55 * proprietary rights relating to intangible property (other than trademark,
56 * trade dress, or service mark rights); and (v) divisions, continuations,
57 * renewals, reissues and extensions of the foregoing (as and to the extent
58 * applicable) now existing, hereafter filed, issued or acquired.
59 *
60 * E. If User commences an infringement action against any Licensor(s) then
61 * such Licensor(s) shall have the right to terminate User's license and
62 * all sublicenses that have been granted hereunder by User to other parties.
63 *
64 * F. This software is for use only in hardware or software products that
65 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license
66 * or right to this Software is granted for products that do not comply
67 * with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased
68 * from the ISO.
69 *
70 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
71 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
72 * THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
73 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING
77 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81 * IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85 * REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
86 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
90 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
94 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
95 * OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
96 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
97 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
98 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
99 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
100 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
101 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
102 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
103 * ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT
105 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
106 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
107 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
108 * NOTICE SPECIFIED IN THIS SECTION.
109 *
110 * __END_OF_JASPER_LICENSE__
111 */
112
113 /*
114 * Bit Stream Class
115 *
116 * $Id$
117 */
118
119 /******************************************************************************\
120 * Includes.
121 \******************************************************************************/
122
123 #include <assert.h>
124 #include <stdlib.h>
125 #include <stdarg.h>
126
127 #include "jasper/jas_malloc.h"
128 #include "jasper/jas_math.h"
129 #include "jasper/jas_debug.h"
130
131 #include "jpc_bs.h"
132
133 /******************************************************************************\
134 * Code for opening and closing bit streams.
135 \******************************************************************************/
136
137 /* Allocate a new bit stream. */
138 static jpc_bitstream_t *jpc_bitstream_alloc()
139 {
140 jpc_bitstream_t *bitstream;
141
142 /* Allocate memory for the new bit stream object. */
143 if (!(bitstream = jas_malloc(sizeof(jpc_bitstream_t)))) {
144 return 0;
145 }
146 /* Initialize all of the data members. */
147 bitstream->stream_ = 0;
148 bitstream->cnt_ = 0;
149 bitstream->flags_ = 0;
150 bitstream->openmode_ = 0;
151
152 return bitstream;
153 }
154
155 /* Open a bit stream from a stream. */
156 jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, const char *mode)
157 {
158 jpc_bitstream_t *bitstream;
159
160 if (!(bitstream = jpc_bitstream_alloc())) {
161 return 0;
162 }
163
164 /* By default, do not close the underlying (character) stream, upon
165 the close of the bit stream. */
166 bitstream->flags_ = JPC_BITSTREAM_NOCLOSE;
167
168 bitstream->stream_ = stream;
169 bitstream->openmode_ = (mode[0] == 'w') ? JPC_BITSTREAM_WRITE :
170 JPC_BITSTREAM_READ;
171
172 /* Mark the data buffer as empty. */
173 bitstream->cnt_ = (bitstream->openmode_ == JPC_BITSTREAM_READ) ? 0 : 8;
174 bitstream->buf_ = 0;
175
176 return bitstream;
177 }
178
179 /* Close a bit stream. */
180 int jpc_bitstream_close(jpc_bitstream_t *bitstream)
181 {
182 int ret = 0;
183
184 /* Align to the next byte boundary while considering the effects of
185 bit stuffing. */
186 if (jpc_bitstream_align(bitstream)) {
187 ret = -1;
188 }
189
190 /* If necessary, close the underlying (character) stream. */
191 if (!(bitstream->flags_ & JPC_BITSTREAM_NOCLOSE) && bitstream->stream_) {
192 if (jas_stream_close(bitstream->stream_)) {
193 ret = -1;
194 }
195 bitstream->stream_ = 0;
196 }
197
198 jas_free(bitstream);
199 return ret;
200 }
201
202 /******************************************************************************\
203 * Code for reading/writing from/to bit streams.
204 \******************************************************************************/
205
206 /* Get a bit from a bit stream. */
207 int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream)
208 {
209 int ret;
210 JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func(%p)\n", bitstream));
211 ret = jpc_bitstream_getbit_macro(bitstream);
212 JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func -> %d\n", ret));
213 return ret;
214 }
215
216 /* Put a bit to a bit stream. */
217 int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int b)
218 {
219 int ret;
220 JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func(%p, %d)\n", bitstream, b));
221 ret = jpc_bitstream_putbit_macro(bitstream, b);
222 JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func() -> %d\n", ret));
223 return ret;
224 }
225
226 /* Get one or more bits from a bit stream. */
227 long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n)
228 {
229 long v;
230 int u;
231
232 /* We can reliably get at most 31 bits since ISO/IEC 9899 only
233 guarantees that a long can represent values up to 2^31-1. */
234 assert(n >= 0 && n < 32);
235
236 /* Get the number of bits requested from the specified bit stream. */
237 v = 0;
238 while (--n >= 0) {
239 if ((u = jpc_bitstream_getbit(bitstream)) < 0) {
240 return -1;
241 }
242 v = (v << 1) | u;
243 }
244 return v;
245 }
246
247 /* Put one or more bits to a bit stream. */
248 int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v)
249 {
250 int m;
251
252 /* We can reliably put at most 31 bits since ISO/IEC 9899 only
253 guarantees that a long can represent values up to 2^31-1. */
254 assert(n >= 0 && n < 32);
255 /* Ensure that only the bits to be output are nonzero. */
256 assert(!(v & (~JAS_ONES(n))));
257
258 /* Put the desired number of bits to the specified bit stream. */
259 m = n - 1;
260 while (--n >= 0) {
261 if (jpc_bitstream_putbit(bitstream, (v >> m) & 1) == EOF) {
262 return EOF;
263 }
264 v <<= 1;
265 }
266 return 0;
267 }
268
269 /******************************************************************************\
270 * Code for buffer filling and flushing.
271 \******************************************************************************/
272
273 /* Fill the buffer for a bit stream. */
274 int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream)
275 {
276 int c;
277 /* Note: The count has already been decremented by the caller. */
278 assert(bitstream->openmode_ & JPC_BITSTREAM_READ);
279 assert(bitstream->cnt_ <= 0);
280
281 if (bitstream->flags_ & JPC_BITSTREAM_ERR) {
282 bitstream->cnt_ = 0;
283 return -1;
284 }
285
286 if (bitstream->flags_ & JPC_BITSTREAM_EOF) {
287 bitstream->buf_ = 0x7f;
288 bitstream->cnt_ = 7;
289 return 1;
290 }
291
292 bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff;
293 if ((c = jas_stream_getc((bitstream)->stream_)) == EOF) {
294 bitstream->flags_ |= JPC_BITSTREAM_EOF;
295 return 1;
296 }
297 bitstream->cnt_ = (bitstream->buf_ == 0xff00) ? 6 : 7;
298 bitstream->buf_ |= c & ((1 << (bitstream->cnt_ + 1)) - 1);
299 return (bitstream->buf_ >> bitstream->cnt_) & 1;
300 }
301
302
303 /******************************************************************************\
304 * Code related to flushing.
305 \******************************************************************************/
306
307 /* Does the bit stream need to be aligned to a byte boundary (considering
308 the effects of bit stuffing)? */
309 int jpc_bitstream_needalign(jpc_bitstream_t *bitstream)
310 {
311 if (bitstream->openmode_ & JPC_BITSTREAM_READ) {
312 /* The bit stream is open for reading. */
313 /* If there are any bits buffered for reading, or the
314 previous byte forced a stuffed bit, alignment is
315 required. */
316 if ((bitstream->cnt_ < 8 && bitstream->cnt_ > 0) ||
317 ((bitstream->buf_ >> 8) & 0xff) == 0xff) {
318 return 1;
319 }
320 } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
321 /* The bit stream is open for writing. */
322 /* If there are any bits buffered for writing, or the
323 previous byte forced a stuffed bit, alignment is
324 required. */
325 if ((bitstream->cnt_ < 8 && bitstream->cnt_ >= 0) ||
326 ((bitstream->buf_ >> 8) & 0xff) == 0xff) {
327 return 1;
328 }
329 } else {
330 /* This should not happen. Famous last words, eh? :-) */
331 assert(0);
332 return -1;
333 }
334 return 0;
335 }
336
337 /* How many additional bytes would be output if we align the bit stream? */
338 int jpc_bitstream_pending(jpc_bitstream_t *bitstream)
339 {
340 if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
341 /* The bit stream is being used for writing. */
342 #if 1
343 /* XXX - Is this really correct? Check someday... */
344 if (bitstream->cnt_ < 8) {
345 return 1;
346 }
347 #else
348 if (bitstream->cnt_ < 8) {
349 if (((bitstream->buf_ >> 8) & 0xff) == 0xff) {
350 return 2;
351 }
352 return 1;
353 }
354 #endif
355 return 0;
356 } else {
357 /* This operation should not be invoked on a bit stream that
358 is being used for reading. */
359 return -1;
360 }
361 }
362
363 /* Align the bit stream to a byte boundary. */
364 int jpc_bitstream_align(jpc_bitstream_t *bitstream)
365 {
366 int ret;
367 if (bitstream->openmode_ & JPC_BITSTREAM_READ) {
368 ret = jpc_bitstream_inalign(bitstream, 0, 0);
369 } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
370 ret = jpc_bitstream_outalign(bitstream, 0);
371 }
372 return ret;
373 }
374
375 /* Align a bit stream in the input case. */
376 int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask,
377 int filldata)
378 {
379 int n;
380 int v;
381 int u;
382 int numfill;
383 int m;
384
385 numfill = 7;
386 m = 0;
387 v = 0;
388 if (bitstream->cnt_ > 0) {
389 n = bitstream->cnt_;
390 } else if (!bitstream->cnt_) {
391 n = ((bitstream->buf_ & 0xff) == 0xff) ? 7 : 0;
392 } else {
393 n = 0;
394 }
395 if (n > 0) {
396 if ((u = jpc_bitstream_getbits(bitstream, n)) < 0) {
397 return -1;
398 }
399 m += n;
400 v = (v << n) | u;
401 }
402 if ((bitstream->buf_ & 0xff) == 0xff) {
403 if ((u = jpc_bitstream_getbits(bitstream, 7)) < 0) {
404 return -1;
405 }
406 v = (v << 7) | u;
407 m += 7;
408 }
409 if (m > numfill) {
410 v >>= m - numfill;
411 } else {
412 filldata >>= numfill - m;
413 fillmask >>= numfill - m;
414 }
415 if (((~(v ^ filldata)) & fillmask) != fillmask) {
416 /* The actual fill pattern does not match the expected one. */
417 return 1;
418 }
419
420 return 0;
421 }
422
423 /* Align a bit stream in the output case. */
424 int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata)
425 {
426 int n;
427 int v;
428
429 /* Ensure that this bit stream is open for writing. */
430 assert(bitstream->openmode_ & JPC_BITSTREAM_WRITE);
431
432 /* Ensure that the first bit of fill data is zero. */
433 /* Note: The first bit of fill data must be zero. If this were not
434 the case, the fill data itself could cause further bit stuffing to
435 be required (which would cause numerous complications). */
436 assert(!(filldata & (~0x3f)));
437
438 if (!bitstream->cnt_) {
439 if ((bitstream->buf_ & 0xff) == 0xff) {
440 n = 7;
441 v = filldata;
442 } else {
443 n = 0;
444 v = 0;
445 }
446 } else if (bitstream->cnt_ > 0 && bitstream->cnt_ < 8) {
447 n = bitstream->cnt_;
448 v = filldata >> (7 - n);
449 } else {
450 n = 0;
451 v = 0;
452 return 0;
453 }
454
455 /* Write the appropriate fill data to the bit stream. */
456 if (n > 0) {
457 if (jpc_bitstream_putbits(bitstream, n, v)) {
458 return -1;
459 }
460 }
461 if (bitstream->cnt_ < 8) {
462 assert(bitstream->cnt_ >= 0 && bitstream->cnt_ < 8);
463 assert((bitstream->buf_ & 0xff) != 0xff);
464 /* Force the pending byte of output to be written to the
465 underlying (character) stream. */
466 if (jas_stream_putc(bitstream->stream_, bitstream->buf_ & 0xff) == EOF) {
467 return -1;
468 }
469 bitstream->cnt_ = 8;
470 bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff;
471 }
472
473 return 0;
474 }