jpc_bs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * 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 permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * Bit Stream Class
  64. *
  65. * $Id: jpc_bs.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include <assert.h>
  71. #include <stdlib.h>
  72. #include <stdarg.h>
  73. #include "jasper/jas_malloc.h"
  74. #include "jasper/jas_math.h"
  75. #include "jasper/jas_debug.h"
  76. #include "jpc_bs.h"
  77. /******************************************************************************\
  78. * Local function prototypes.
  79. \******************************************************************************/
  80. static jpc_bitstream_t *jpc_bitstream_alloc(void);
  81. /******************************************************************************\
  82. * Code for opening and closing bit streams.
  83. \******************************************************************************/
  84. /* Open a bit stream from a stream. */
  85. jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, char *mode)
  86. {
  87. jpc_bitstream_t *bitstream;
  88. /* Ensure that the open mode is valid. */
  89. #if 1
  90. /* This causes a string literal too long error (with c99 pedantic mode). */
  91. assert(!strcmp(mode, "r") || !strcmp(mode, "w") || !strcmp(mode, "r+")
  92. || !strcmp(mode, "w+"));
  93. #endif
  94. if (!(bitstream = jpc_bitstream_alloc())) {
  95. return 0;
  96. }
  97. /* By default, do not close the underlying (character) stream, upon
  98. the close of the bit stream. */
  99. bitstream->flags_ = JPC_BITSTREAM_NOCLOSE;
  100. bitstream->stream_ = stream;
  101. bitstream->openmode_ = (mode[0] == 'w') ? JPC_BITSTREAM_WRITE :
  102. JPC_BITSTREAM_READ;
  103. /* Mark the data buffer as empty. */
  104. bitstream->cnt_ = (bitstream->openmode_ == JPC_BITSTREAM_READ) ? 0 : 8;
  105. bitstream->buf_ = 0;
  106. return bitstream;
  107. }
  108. /* Close a bit stream. */
  109. int jpc_bitstream_close(jpc_bitstream_t *bitstream)
  110. {
  111. int ret = 0;
  112. /* Align to the next byte boundary while considering the effects of
  113. bit stuffing. */
  114. if (jpc_bitstream_align(bitstream)) {
  115. ret = -1;
  116. }
  117. /* If necessary, close the underlying (character) stream. */
  118. if (!(bitstream->flags_ & JPC_BITSTREAM_NOCLOSE) && bitstream->stream_) {
  119. if (jas_stream_close(bitstream->stream_)) {
  120. ret = -1;
  121. }
  122. bitstream->stream_ = 0;
  123. }
  124. jas_free(bitstream);
  125. return ret;
  126. }
  127. /* Allocate a new bit stream. */
  128. static jpc_bitstream_t *jpc_bitstream_alloc()
  129. {
  130. jpc_bitstream_t *bitstream;
  131. /* Allocate memory for the new bit stream object. */
  132. if (!(bitstream = jas_malloc(sizeof(jpc_bitstream_t)))) {
  133. return 0;
  134. }
  135. /* Initialize all of the data members. */
  136. bitstream->stream_ = 0;
  137. bitstream->cnt_ = 0;
  138. bitstream->flags_ = 0;
  139. bitstream->openmode_ = 0;
  140. return bitstream;
  141. }
  142. /******************************************************************************\
  143. * Code for reading/writing from/to bit streams.
  144. \******************************************************************************/
  145. /* Get a bit from a bit stream. */
  146. int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream)
  147. {
  148. int ret;
  149. JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func(%p)\n", bitstream));
  150. ret = jpc_bitstream_getbit_macro(bitstream);
  151. JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func -> %d\n", ret));
  152. return ret;
  153. }
  154. /* Put a bit to a bit stream. */
  155. int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int b)
  156. {
  157. int ret;
  158. JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func(%p, %d)\n", bitstream, b));
  159. ret = jpc_bitstream_putbit_macro(bitstream, b);
  160. JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func() -> %d\n", ret));
  161. return ret;
  162. }
  163. /* Get one or more bits from a bit stream. */
  164. long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n)
  165. {
  166. long v;
  167. int u;
  168. /* We can reliably get at most 31 bits since ISO/IEC 9899 only
  169. guarantees that a long can represent values up to 2^31-1. */
  170. assert(n >= 0 && n < 32);
  171. /* Get the number of bits requested from the specified bit stream. */
  172. v = 0;
  173. while (--n >= 0) {
  174. if ((u = jpc_bitstream_getbit(bitstream)) < 0) {
  175. return -1;
  176. }
  177. v = (v << 1) | u;
  178. }
  179. return v;
  180. }
  181. /* Put one or more bits to a bit stream. */
  182. int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v)
  183. {
  184. int m;
  185. /* We can reliably put at most 31 bits since ISO/IEC 9899 only
  186. guarantees that a long can represent values up to 2^31-1. */
  187. assert(n >= 0 && n < 32);
  188. /* Ensure that only the bits to be output are nonzero. */
  189. assert(!(v & (~JAS_ONES(n))));
  190. /* Put the desired number of bits to the specified bit stream. */
  191. m = n - 1;
  192. while (--n >= 0) {
  193. if (jpc_bitstream_putbit(bitstream, (v >> m) & 1) == EOF) {
  194. return EOF;
  195. }
  196. v <<= 1;
  197. }
  198. return 0;
  199. }
  200. /******************************************************************************\
  201. * Code for buffer filling and flushing.
  202. \******************************************************************************/
  203. /* Fill the buffer for a bit stream. */
  204. int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream)
  205. {
  206. int c;
  207. /* Note: The count has already been decremented by the caller. */
  208. assert(bitstream->openmode_ & JPC_BITSTREAM_READ);
  209. assert(bitstream->cnt_ <= 0);
  210. if (bitstream->flags_ & JPC_BITSTREAM_ERR) {
  211. bitstream->cnt_ = 0;
  212. return -1;
  213. }
  214. if (bitstream->flags_ & JPC_BITSTREAM_EOF) {
  215. bitstream->buf_ = 0x7f;
  216. bitstream->cnt_ = 7;
  217. return 1;
  218. }
  219. bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff;
  220. if ((c = jas_stream_getc((bitstream)->stream_)) == EOF) {
  221. bitstream->flags_ |= JPC_BITSTREAM_EOF;
  222. return 1;
  223. }
  224. bitstream->cnt_ = (bitstream->buf_ == 0xff00) ? 6 : 7;
  225. bitstream->buf_ |= c & ((1 << (bitstream->cnt_ + 1)) - 1);
  226. return (bitstream->buf_ >> bitstream->cnt_) & 1;
  227. }
  228. /******************************************************************************\
  229. * Code related to flushing.
  230. \******************************************************************************/
  231. /* Does the bit stream need to be aligned to a byte boundary (considering
  232. the effects of bit stuffing)? */
  233. int jpc_bitstream_needalign(jpc_bitstream_t *bitstream)
  234. {
  235. if (bitstream->openmode_ & JPC_BITSTREAM_READ) {
  236. /* The bit stream is open for reading. */
  237. /* If there are any bits buffered for reading, or the
  238. previous byte forced a stuffed bit, alignment is
  239. required. */
  240. if ((bitstream->cnt_ < 8 && bitstream->cnt_ > 0) ||
  241. ((bitstream->buf_ >> 8) & 0xff) == 0xff) {
  242. return 1;
  243. }
  244. } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
  245. /* The bit stream is open for writing. */
  246. /* If there are any bits buffered for writing, or the
  247. previous byte forced a stuffed bit, alignment is
  248. required. */
  249. if ((bitstream->cnt_ < 8 && bitstream->cnt_ >= 0) ||
  250. ((bitstream->buf_ >> 8) & 0xff) == 0xff) {
  251. return 1;
  252. }
  253. } else {
  254. /* This should not happen. Famous last words, eh? :-) */
  255. assert(0);
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. /* How many additional bytes would be output if we align the bit stream? */
  261. int jpc_bitstream_pending(jpc_bitstream_t *bitstream)
  262. {
  263. if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
  264. /* The bit stream is being used for writing. */
  265. #if 1
  266. /* XXX - Is this really correct? Check someday... */
  267. if (bitstream->cnt_ < 8) {
  268. return 1;
  269. }
  270. #else
  271. if (bitstream->cnt_ < 8) {
  272. if (((bitstream->buf_ >> 8) & 0xff) == 0xff) {
  273. return 2;
  274. }
  275. return 1;
  276. }
  277. #endif
  278. return 0;
  279. } else {
  280. /* This operation should not be invoked on a bit stream that
  281. is being used for reading. */
  282. return -1;
  283. }
  284. }
  285. /* Align the bit stream to a byte boundary. */
  286. int jpc_bitstream_align(jpc_bitstream_t *bitstream)
  287. {
  288. int ret;
  289. if (bitstream->openmode_ & JPC_BITSTREAM_READ) {
  290. ret = jpc_bitstream_inalign(bitstream, 0, 0);
  291. } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) {
  292. ret = jpc_bitstream_outalign(bitstream, 0);
  293. } else {
  294. abort();
  295. }
  296. return ret;
  297. }
  298. /* Align a bit stream in the input case. */
  299. int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask,
  300. int filldata)
  301. {
  302. int n;
  303. int v;
  304. int u;
  305. int numfill;
  306. int m;
  307. numfill = 7;
  308. m = 0;
  309. v = 0;
  310. if (bitstream->cnt_ > 0) {
  311. n = bitstream->cnt_;
  312. } else if (!bitstream->cnt_) {
  313. n = ((bitstream->buf_ & 0xff) == 0xff) ? 7 : 0;
  314. } else {
  315. n = 0;
  316. }
  317. if (n > 0) {
  318. if ((u = jpc_bitstream_getbits(bitstream, n)) < 0) {
  319. return -1;
  320. }
  321. m += n;
  322. v = (v << n) | u;
  323. }
  324. if ((bitstream->buf_ & 0xff) == 0xff) {
  325. if ((u = jpc_bitstream_getbits(bitstream, 7)) < 0) {
  326. return -1;
  327. }
  328. v = (v << 7) | u;
  329. m += 7;
  330. }
  331. if (m > numfill) {
  332. v >>= m - numfill;
  333. } else {
  334. filldata >>= numfill - m;
  335. fillmask >>= numfill - m;
  336. }
  337. if (((~(v ^ filldata)) & fillmask) != fillmask) {
  338. /* The actual fill pattern does not match the expected one. */
  339. return 1;
  340. }
  341. return 0;
  342. }
  343. /* Align a bit stream in the output case. */
  344. int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata)
  345. {
  346. int n;
  347. int v;
  348. /* Ensure that this bit stream is open for writing. */
  349. assert(bitstream->openmode_ & JPC_BITSTREAM_WRITE);
  350. /* Ensure that the first bit of fill data is zero. */
  351. /* Note: The first bit of fill data must be zero. If this were not
  352. the case, the fill data itself could cause further bit stuffing to
  353. be required (which would cause numerous complications). */
  354. assert(!(filldata & (~0x3f)));
  355. if (!bitstream->cnt_) {
  356. if ((bitstream->buf_ & 0xff) == 0xff) {
  357. n = 7;
  358. v = filldata;
  359. } else {
  360. n = 0;
  361. v = 0;
  362. }
  363. } else if (bitstream->cnt_ > 0 && bitstream->cnt_ < 8) {
  364. n = bitstream->cnt_;
  365. v = filldata >> (7 - n);
  366. } else {
  367. n = 0;
  368. v = 0;
  369. return 0;
  370. }
  371. /* Write the appropriate fill data to the bit stream. */
  372. if (n > 0) {
  373. if (jpc_bitstream_putbits(bitstream, n, v)) {
  374. return -1;
  375. }
  376. }
  377. if (bitstream->cnt_ < 8) {
  378. assert(bitstream->cnt_ >= 0 && bitstream->cnt_ < 8);
  379. assert((bitstream->buf_ & 0xff) != 0xff);
  380. /* Force the pending byte of output to be written to the
  381. underlying (character) stream. */
  382. if (jas_stream_putc(bitstream->stream_, bitstream->buf_ & 0xff) == EOF) {
  383. return -1;
  384. }
  385. bitstream->cnt_ = 8;
  386. bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff;
  387. }
  388. return 0;
  389. }