jpegdecutils.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Description :Jpeg Decoder utils
  17. --
  18. ------------------------------------------------------------------------------
  19. --
  20. -- Version control information, please leave untouched.
  21. --
  22. -- $RCSfile: jpegdecutils.c,v $
  23. -- $Revision: 1.1 $
  24. -- $Date: 2007/03/30 05:44:50 $
  25. --
  26. ------------------------------------------------------------------------------*/
  27. /*------------------------------------------------------------------------------
  28. Table of contents
  29. 1. Include headers
  30. 2. External compiler flags
  31. 3. Module defines
  32. 4. Local function prototypes
  33. 5. Functions
  34. - JpegDecGetByte
  35. - JpegDecGet2Bytes
  36. - JpegDecShowBits
  37. - JpegDecFlushBits
  38. ------------------------------------------------------------------------------*/
  39. /*------------------------------------------------------------------------------
  40. 1. Include headers
  41. ------------------------------------------------------------------------------*/
  42. #include <linux/module.h>
  43. #include <linux/platform_device.h>
  44. #include "jpegdecutils.h"
  45. #include "jpegdecmarkers.h"
  46. /*------------------------------------------------------------------------------
  47. 2. External compiler flags
  48. --------------------------------------------------------------------------------
  49. --------------------------------------------------------------------------------
  50. 3. Module defines
  51. ------------------------------------------------------------------------------*/
  52. /*------------------------------------------------------------------------------
  53. 4. Local function prototypes
  54. ------------------------------------------------------------------------------*/
  55. /*------------------------------------------------------------------------------
  56. 5. Functions
  57. ------------------------------------------------------------------------------*/
  58. /*------------------------------------------------------------------------------
  59. Function name: JpegDecGetByte
  60. Functional description:
  61. Reads one byte (8 bits) from stream and returns the bits
  62. Note! This function does not skip the 0x00 byte if the previous
  63. byte value was 0xFF!!!
  64. Inputs:
  65. StreamStorage *pStream Pointer to structure
  66. Outputs:
  67. Returns 8 bit value if ok
  68. else returns STRM_ERROR (0xFFFFFFFF)
  69. ------------------------------------------------------------------------------*/
  70. u32 JpegDecGetByte(StreamStorage * pStream)
  71. {
  72. u32 tmp;
  73. if((pStream->readBits + 8) > (8 * pStream->streamLength))
  74. return (STRM_ERROR);
  75. tmp = *(pStream->pCurrPos)++;
  76. tmp = (tmp << 8) | *(pStream->pCurrPos);
  77. tmp = (tmp >> (8 - pStream->bitPosInByte)) & 0xFF;
  78. pStream->readBits += 8;
  79. return (tmp);
  80. }
  81. /*------------------------------------------------------------------------------
  82. Function name: JpegDecGet2Bytes
  83. Functional description:
  84. Reads two bytes (16 bits) from stream and returns the bits
  85. Note! This function does not skip the 0x00 byte if the previous
  86. byte value was 0xFF!!!
  87. Inputs:
  88. StreamStorage *pStream Pointer to structure
  89. Outputs:
  90. Returns 16 bit value
  91. ------------------------------------------------------------------------------*/
  92. u32 JpegDecGet2Bytes(StreamStorage * pStream)
  93. {
  94. u32 tmp;
  95. if((pStream->readBits + 16) > (8 * pStream->streamLength))
  96. return (STRM_ERROR);
  97. tmp = *(pStream->pCurrPos)++;
  98. tmp = (tmp << 8) | *(pStream->pCurrPos)++;
  99. tmp = (tmp << 8) | *(pStream->pCurrPos);
  100. tmp = (tmp >> (8 - pStream->bitPosInByte)) & 0xFFFF;
  101. pStream->readBits += 16;
  102. return (tmp);
  103. }
  104. /*------------------------------------------------------------------------------
  105. Function name: JpegDecShowBits
  106. Functional description:
  107. Reads 32 bits from stream and returns the bits, does not update
  108. stream pointers. If there are not enough bits in data buffer it
  109. reads the rest of the data buffer bits and fills the lsb of return
  110. value with zero bits.
  111. Note! This function will skip the byte valued 0x00 if the previous
  112. byte value was 0xFF!!!
  113. Inputs:
  114. StreamStorage *pStream Pointer to structure
  115. Outputs:
  116. Returns 32 bit value
  117. ------------------------------------------------------------------------------*/
  118. u32 JpegDecShowBits(StreamStorage * pStream)
  119. {
  120. i32 bits;
  121. u32 readBits;
  122. u32 out = 0;
  123. u8 *pData = pStream->pCurrPos;
  124. /* bits left in buffer */
  125. bits = (i32) (8 * pStream->streamLength - pStream->readBits);
  126. if(!bits)
  127. return (0);
  128. readBits = 0;
  129. do
  130. {
  131. if(pData > pStream->pStartOfStream)
  132. {
  133. /* FF 00 bytes in stream -> jump over 00 byte */
  134. if((pData[-1] == 0xFF) && (pData[0] == 0x00))
  135. {
  136. pData++;
  137. bits -= 8;
  138. }
  139. }
  140. if(readBits == 32 && pStream->bitPosInByte)
  141. {
  142. out <<= pStream->bitPosInByte;
  143. out |= *pData >> (8 - pStream->bitPosInByte);
  144. readBits = 0;
  145. break;
  146. }
  147. out = (out << 8) | *pData++;
  148. readBits += 8;
  149. bits -= 8;
  150. }
  151. while(readBits < (32 + pStream->bitPosInByte) && bits > 0);
  152. if(bits <= 0 &&
  153. ((readBits + pStream->readBits) >= (pStream->streamLength * 8)))
  154. {
  155. /* not enough bits in stream, fill with zeros */
  156. out = (out << (32 - (readBits - pStream->bitPosInByte)));
  157. }
  158. return (out);
  159. }
  160. /*------------------------------------------------------------------------------
  161. Function name: JpegDecFlushBits
  162. Functional description:
  163. Updates stream pointers, flushes bits from stream
  164. Note! This function will skip the byte valued 0x00 if the previous
  165. byte value was 0xFF!!!
  166. Inputs:
  167. StreamStorage *pStream Pointer to structure
  168. u32 bits Number of bits to be flushed
  169. Outputs:
  170. OK
  171. STRM_ERROR
  172. ------------------------------------------------------------------------------*/
  173. u32 JpegDecFlushBits(StreamStorage * pStream, u32 bits)
  174. {
  175. u32 tmp;
  176. u32 extraBits = 0;
  177. if((pStream->readBits + bits) > (8 * pStream->streamLength))
  178. {
  179. /* there are not so many bits left in buffer */
  180. /* stream pointers to the end of the stream */
  181. /* and return value STRM_ERROR */
  182. pStream->readBits = 8 * pStream->streamLength;
  183. pStream->bitPosInByte = 0;
  184. pStream->pCurrPos = pStream->pStartOfStream + pStream->streamLength;
  185. return (STRM_ERROR);
  186. }
  187. else
  188. {
  189. tmp = 0;
  190. while(tmp < bits)
  191. {
  192. if(bits - tmp < 8)
  193. {
  194. if((8 - pStream->bitPosInByte) > (bits - tmp))
  195. {
  196. /* inside one byte */
  197. pStream->bitPosInByte += bits - tmp;
  198. tmp = bits;
  199. }
  200. else
  201. {
  202. if(pStream->pCurrPos[0] == 0xFF &&
  203. pStream->pCurrPos[1] == 0x00)
  204. {
  205. extraBits += 8;
  206. pStream->pCurrPos += 2;
  207. }
  208. else
  209. {
  210. pStream->pCurrPos++;
  211. }
  212. tmp += 8 - pStream->bitPosInByte;
  213. pStream->bitPosInByte = 0;
  214. pStream->bitPosInByte = bits - tmp;
  215. tmp = bits;
  216. }
  217. }
  218. else
  219. {
  220. tmp += 8;
  221. if(pStream->appnFlag)
  222. {
  223. pStream->pCurrPos++;
  224. }
  225. else
  226. {
  227. if(pStream->pCurrPos[0] == 0xFF &&
  228. pStream->pCurrPos[1] == 0x00)
  229. {
  230. extraBits += 8;
  231. pStream->pCurrPos += 2;
  232. }
  233. else
  234. {
  235. pStream->pCurrPos++;
  236. }
  237. }
  238. }
  239. }
  240. /* update stream pointers */
  241. pStream->readBits += bits + extraBits;
  242. return (OK);
  243. }
  244. }