vc1hwd_stream.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 : Stream accessing
  17. --
  18. --------------------------------------------------------------------------------
  19. --
  20. -- Version control information, please leave untouched.
  21. --
  22. -- $RCSfile: vc1hwd_stream.c,v $
  23. -- $Revision: 1.10 $
  24. -- $Date: 2007/12/12 13:25:58 $
  25. --
  26. ------------------------------------------------------------------------------*/
  27. /*------------------------------------------------------------------------------
  28. Include headers
  29. ------------------------------------------------------------------------------*/
  30. #include "vc1hwd_util.h"
  31. #include "vc1hwd_stream.h"
  32. /*------------------------------------------------------------------------------
  33. External compiler flags
  34. ------------------------------------------------------------------------------*/
  35. /*------------------------------------------------------------------------------
  36. Module defines
  37. ------------------------------------------------------------------------------*/
  38. /*------------------------------------------------------------------------------
  39. Local function prototypes
  40. ------------------------------------------------------------------------------*/
  41. static u32 SwShowBits32(strmData_t *pStrmData);
  42. static u32 SwFlushBits(strmData_t *pStrmData, u32 numBits);
  43. static u32 SwFlushBitsAdv(strmData_t *pStrmData, u32 numBits);
  44. /*------------------------------------------------------------------------------
  45. Functions
  46. ------------------------------------------------------------------------------*/
  47. /*------------------------------------------------------------------------------
  48. Function: vc1hwdGetBits
  49. Functional description:
  50. Read and remove bits from the stream buffer.
  51. Input:
  52. pStrmData pointer to stream data structure
  53. numBits number of bits to read
  54. Output:
  55. none
  56. Returns:
  57. bits read from stream
  58. END_OF_STREAM if not enough bits left
  59. ------------------------------------------------------------------------------*/
  60. u32 vc1hwdGetBits(strmData_t *pStrmData, u32 numBits)
  61. {
  62. u32 out;
  63. ASSERT(pStrmData);
  64. if (pStrmData->removeEmulPrevBytes)
  65. out = vc1hwdShowBits( pStrmData, numBits );
  66. else
  67. out = (SwShowBits32(pStrmData) >> (32 - numBits));
  68. if (vc1hwdFlushBits(pStrmData, numBits) == HANTRO_OK)
  69. {
  70. return(out);
  71. }
  72. else
  73. {
  74. return(END_OF_STREAM);
  75. }
  76. }
  77. /*------------------------------------------------------------------------------
  78. Function: vc1hwdShowBits
  79. Functional description:
  80. Read bits from the stream buffer. Buffer is left as it is, i.e.
  81. no bits are removed. First bit read from the stream is the MSB of
  82. the return value. If there is not enough bits in the buffer ->
  83. bits beyong the end of the stream are set to '0' in the return
  84. value.
  85. Input:
  86. pStrmData pointer to stream data structure
  87. Output:
  88. none
  89. Returns:
  90. bits read from stream
  91. ------------------------------------------------------------------------------*/
  92. u32 vc1hwdShowBits(strmData_t *pStrmData, u32 numBits )
  93. {
  94. i32 bits;
  95. u32 out, outBits;
  96. u32 tmpReadBits;
  97. u8 *pStrm;
  98. ASSERT(pStrmData);
  99. ASSERT(pStrmData->pStrmCurrPos);
  100. ASSERT(pStrmData->bitPosInWord < 8);
  101. ASSERT(pStrmData->bitPosInWord ==
  102. (pStrmData->strmBuffReadBits & 0x7));
  103. ASSERT(numBits <= 32);
  104. pStrm = pStrmData->pStrmCurrPos;
  105. /* bits left in the buffer */
  106. bits = (i32)pStrmData->strmBuffSize*8 - (i32)pStrmData->strmBuffReadBits;
  107. if (!numBits || !bits)
  108. {
  109. return(0);
  110. }
  111. out = outBits = 0;
  112. tmpReadBits = pStrmData->strmBuffReadBits;
  113. if (pStrmData->bitPosInWord)
  114. {
  115. out = pStrm[0]<<(24+pStrmData->bitPosInWord);
  116. pStrm++;
  117. outBits = 8-pStrmData->bitPosInWord;
  118. bits -= outBits;
  119. tmpReadBits += outBits;
  120. }
  121. while (bits && outBits < numBits)
  122. {
  123. /* check emulation prevention byte */
  124. if (pStrmData->removeEmulPrevBytes && tmpReadBits >= 16 &&
  125. pStrm[-2] == 0x0 && pStrm[-1] == 0x0 && pStrm[0] == 0x3)
  126. {
  127. pStrm++;
  128. tmpReadBits += 8;
  129. bits -= 8;
  130. /* emulation prevention byte shall not be the last byte of the
  131. * stream */
  132. if (bits <= 0)
  133. break;
  134. }
  135. if (outBits <= 24)
  136. out |= (u32)(*pStrm++) << (24 - outBits);
  137. else
  138. out |= (u32)(*pStrm++) >> (outBits-24);
  139. outBits += 8;
  140. tmpReadBits += 8;
  141. bits -= 8;
  142. }
  143. return(out>>(32-numBits));
  144. }
  145. /*------------------------------------------------------------------------------
  146. Function: SwShowBits32
  147. Functional description:
  148. Read 32 bits from the stream buffer when emulation prevention bytes
  149. are not present in the bitstream. Buffer is left as it is, i.e.
  150. no bits are removed. First bit read from the stream is the MSB of
  151. the return value. If there is not enough bits in the buffer ->
  152. bits beyong the end of the stream are set to '0' in the return
  153. value.
  154. Input:
  155. pStrmData pointer to stream data structure
  156. Output:
  157. none
  158. Returns:
  159. bits read from stream
  160. ------------------------------------------------------------------------------*/
  161. u32 SwShowBits32(strmData_t *pStrmData)
  162. {
  163. i32 bits, shift;
  164. u32 out;
  165. u8 *pStrm;
  166. ASSERT(pStrmData);
  167. ASSERT(pStrmData->pStrmCurrPos);
  168. ASSERT(pStrmData->bitPosInWord < 8);
  169. ASSERT(pStrmData->bitPosInWord ==
  170. (pStrmData->strmBuffReadBits & 0x7));
  171. pStrm = pStrmData->pStrmCurrPos;
  172. /* number of bits left in the buffer */
  173. bits = (i32)pStrmData->strmBuffSize*8 - (i32)pStrmData->strmBuffReadBits;
  174. /* at least 32-bits in the buffer */
  175. if (bits >= 32)
  176. {
  177. u32 bitPosInWord = pStrmData->bitPosInWord;
  178. out = ((u32)pStrm[3]) | ((u32)pStrm[2] << 8) |
  179. ((u32)pStrm[1] << 16) | ((u32)pStrm[0] << 24);
  180. if (bitPosInWord)
  181. {
  182. out <<= bitPosInWord;
  183. out |= (u32)pStrm[4]>>(8-bitPosInWord);
  184. }
  185. return (out);
  186. }
  187. /* at least one bit in the buffer */
  188. else if (bits > 0)
  189. {
  190. shift = (i32)(24 + pStrmData->bitPosInWord);
  191. out = (u32)(*pStrm++) << shift;
  192. bits -= (i32)(8 - pStrmData->bitPosInWord);
  193. while (bits > 0)
  194. {
  195. shift -= 8;
  196. out |= (u32)(*pStrm++) << shift;
  197. bits -= 8;
  198. }
  199. return (out);
  200. }
  201. else
  202. return (0);
  203. }
  204. /*------------------------------------------------------------------------------
  205. Function: vc1hwdFlushBits
  206. Functional description:
  207. Remove bits from the stream buffer
  208. Input:
  209. pStrmData pointer to stream data structure
  210. numBits number of bits to remove
  211. Output:
  212. none
  213. Returns:
  214. HANTRO_OK success
  215. END_OF_STREAM not enough bits left
  216. ------------------------------------------------------------------------------*/
  217. u32 vc1hwdFlushBits(strmData_t *pStrmData, u32 numBits)
  218. {
  219. u32 rv;
  220. ASSERT(pStrmData);
  221. ASSERT(pStrmData->pStrmBuffStart);
  222. ASSERT(pStrmData->pStrmCurrPos);
  223. ASSERT(pStrmData->bitPosInWord < 8);
  224. ASSERT(pStrmData->bitPosInWord == (pStrmData->strmBuffReadBits & 0x7));
  225. if (pStrmData->removeEmulPrevBytes)
  226. rv = SwFlushBitsAdv( pStrmData, numBits );
  227. else
  228. rv = SwFlushBits(pStrmData, numBits);
  229. return rv;
  230. }
  231. /*------------------------------------------------------------------------------
  232. Function: SwFlushBits
  233. Functional description:
  234. Remove bits from the stream buffer for streams that are not
  235. containing emulation prevention bytes
  236. Input:
  237. pStrmData pointer to stream data structure
  238. numBits number of bits to remove
  239. Output:
  240. none
  241. Returns:
  242. HANTRO_OK success
  243. END_OF_STREAM not enough bits left
  244. ------------------------------------------------------------------------------*/
  245. u32 SwFlushBits(strmData_t *pStrmData, u32 numBits)
  246. {
  247. ASSERT(pStrmData);
  248. ASSERT(pStrmData->pStrmBuffStart);
  249. ASSERT(pStrmData->pStrmCurrPos);
  250. ASSERT(pStrmData->bitPosInWord < 8);
  251. ASSERT(pStrmData->bitPosInWord == (pStrmData->strmBuffReadBits & 0x7));
  252. pStrmData->strmBuffReadBits += numBits;
  253. pStrmData->bitPosInWord = pStrmData->strmBuffReadBits & 0x7;
  254. if ( (pStrmData->strmBuffReadBits ) <= (8*pStrmData->strmBuffSize) )
  255. {
  256. pStrmData->pStrmCurrPos = pStrmData->pStrmBuffStart +
  257. (pStrmData->strmBuffReadBits >> 3);
  258. return(HANTRO_OK);
  259. }
  260. else
  261. {
  262. pStrmData->strmExhausted = HANTRO_TRUE;
  263. return(END_OF_STREAM);
  264. }
  265. }
  266. /*------------------------------------------------------------------------------
  267. Function: SwFlushBitsAdv
  268. Functional description:
  269. Remove bits from the stream buffer for streams with emulation
  270. prevention bytes.
  271. Input:
  272. pStrmData pointer to stream data structure
  273. numBits number of bits to remove
  274. Output:
  275. none
  276. Returns:
  277. HANTRO_OK success
  278. END_OF_STREAM not enough bits left
  279. ------------------------------------------------------------------------------*/
  280. u32 SwFlushBitsAdv(strmData_t *pStrmData, u32 numBits)
  281. {
  282. u32 bytesLeft;
  283. u8 *pStrm;
  284. ASSERT(pStrmData);
  285. ASSERT(pStrmData->pStrmBuffStart);
  286. ASSERT(pStrmData->pStrmCurrPos);
  287. ASSERT(pStrmData->bitPosInWord < 8);
  288. ASSERT(pStrmData->bitPosInWord == (pStrmData->strmBuffReadBits & 0x7));
  289. if ( (pStrmData->strmBuffReadBits + numBits) > (8*pStrmData->strmBuffSize) )
  290. {
  291. pStrmData->strmBuffReadBits = 8 * pStrmData->strmBuffSize;
  292. pStrmData->bitPosInWord = 0;
  293. pStrmData->pStrmCurrPos =
  294. pStrmData->pStrmBuffStart + pStrmData->strmBuffSize;
  295. pStrmData->strmExhausted = HANTRO_TRUE;
  296. return(END_OF_STREAM);
  297. }
  298. else
  299. {
  300. bytesLeft = (8*pStrmData->strmBuffSize-pStrmData->strmBuffReadBits)/8;
  301. pStrm = pStrmData->pStrmCurrPos;
  302. if (pStrmData->bitPosInWord)
  303. {
  304. if (numBits < 8-pStrmData->bitPosInWord)
  305. {
  306. pStrmData->strmBuffReadBits += numBits;
  307. pStrmData->bitPosInWord += numBits;
  308. return(HANTRO_OK);
  309. }
  310. numBits -= 8-pStrmData->bitPosInWord;
  311. pStrmData->strmBuffReadBits += 8-pStrmData->bitPosInWord;
  312. pStrmData->bitPosInWord = 0;
  313. pStrm++;
  314. if (pStrmData->strmBuffReadBits >= 16 && bytesLeft &&
  315. pStrm[-2] == 0x0 && pStrm[-1] == 0x0 && pStrm[0] == 0x3)
  316. {
  317. pStrm++;
  318. pStrmData->strmBuffReadBits += 8;
  319. bytesLeft--;
  320. }
  321. }
  322. while (numBits >= 8 && bytesLeft)
  323. {
  324. pStrm++;
  325. pStrmData->strmBuffReadBits += 8;
  326. bytesLeft--;
  327. /* check emulation prevention byte */
  328. if (pStrmData->strmBuffReadBits >= 16 && bytesLeft &&
  329. pStrm[-2] == 0x0 && pStrm[-1] == 0x0 && pStrm[0] == 0x3)
  330. {
  331. pStrm++;
  332. pStrmData->strmBuffReadBits += 8;
  333. bytesLeft--;
  334. }
  335. numBits -= 8;
  336. }
  337. if (numBits && bytesLeft)
  338. {
  339. pStrmData->strmBuffReadBits += numBits;
  340. pStrmData->bitPosInWord = numBits;
  341. numBits = 0;
  342. }
  343. pStrmData->pStrmCurrPos = pStrm;
  344. if (numBits)
  345. {
  346. pStrmData->strmExhausted = HANTRO_TRUE;
  347. return(END_OF_STREAM);
  348. }
  349. else
  350. return(HANTRO_OK);
  351. }
  352. }
  353. /*------------------------------------------------------------------------------
  354. Function: vc1hwdIsExhausted
  355. Functional description:
  356. Check if attempted to read more bits from stream than there are
  357. available.
  358. Inputs:
  359. pStrmData pointer to stream data structure
  360. Outputs:
  361. None
  362. Returns:
  363. TRUE stream is exhausted.
  364. FALSE stream is not exhausted.
  365. ------------------------------------------------------------------------------*/
  366. u32 vc1hwdIsExhausted(const strmData_t * const pStrmData)
  367. {
  368. return pStrmData->strmExhausted;
  369. }