PageRenderTime 42ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/H264Dec/source/h264bsd_stream.c

http://github.com/mbebenita/Broadway
C | 242 lines | 86 code | 31 blank | 125 comment | 10 complexity | 04af0b1338f516da6161b1546a8ef4d6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*------------------------------------------------------------------------------
  17. Table of contents
  18. 1. Include headers
  19. 2. External compiler flags
  20. 3. Module defines
  21. 4. Local function prototypes
  22. 5. Functions
  23. h264bsdGetBits
  24. h264bsdShowBits32
  25. h264bsdFlushBits
  26. h264bsdIsByteAligned
  27. ------------------------------------------------------------------------------*/
  28. /*------------------------------------------------------------------------------
  29. 1. Include headers
  30. ------------------------------------------------------------------------------*/
  31. #include "h264bsd_util.h"
  32. #include "h264bsd_stream.h"
  33. /*------------------------------------------------------------------------------
  34. 2. External compiler flags
  35. --------------------------------------------------------------------------------
  36. --------------------------------------------------------------------------------
  37. 3. Module defines
  38. ------------------------------------------------------------------------------*/
  39. /*------------------------------------------------------------------------------
  40. 4. Local function prototypes
  41. ------------------------------------------------------------------------------*/
  42. /*------------------------------------------------------------------------------
  43. Function: h264bsdGetBits
  44. Functional description:
  45. Read and remove bits from the stream buffer.
  46. Input:
  47. pStrmData pointer to stream data structure
  48. numBits number of bits to read
  49. Output:
  50. none
  51. Returns:
  52. bits read from stream
  53. END_OF_STREAM if not enough bits left
  54. ------------------------------------------------------------------------------*/
  55. u32 h264bsdGetBits(strmData_t *pStrmData, u32 numBits)
  56. {
  57. u32 out;
  58. ASSERT(pStrmData);
  59. ASSERT(numBits < 32);
  60. out = h264bsdShowBits32(pStrmData) >> (32 - numBits);
  61. if (h264bsdFlushBits(pStrmData, numBits) == HANTRO_OK)
  62. {
  63. return(out);
  64. }
  65. else
  66. {
  67. return(END_OF_STREAM);
  68. }
  69. }
  70. /*------------------------------------------------------------------------------
  71. Function: h264bsdShowBits32
  72. Functional description:
  73. Read 32 bits from the stream buffer. Buffer is left as it is, i.e.
  74. no bits are removed. First bit read from the stream is the MSB of
  75. the return value. If there is not enough bits in the buffer ->
  76. bits beyong the end of the stream are set to '0' in the return
  77. value.
  78. Input:
  79. pStrmData pointer to stream data structure
  80. Output:
  81. none
  82. Returns:
  83. bits read from stream
  84. ------------------------------------------------------------------------------*/
  85. u32 h264bsdShowBits32(strmData_t *pStrmData)
  86. {
  87. i32 bits, shift;
  88. u32 out;
  89. u8 *pStrm;
  90. ASSERT(pStrmData);
  91. ASSERT(pStrmData->pStrmCurrPos);
  92. ASSERT(pStrmData->bitPosInWord < 8);
  93. ASSERT(pStrmData->bitPosInWord ==
  94. (pStrmData->strmBuffReadBits & 0x7));
  95. pStrm = pStrmData->pStrmCurrPos;
  96. /* number of bits left in the buffer */
  97. bits = (i32)pStrmData->strmBuffSize*8 - (i32)pStrmData->strmBuffReadBits;
  98. /* at least 32-bits in the buffer */
  99. if (bits >= 32)
  100. {
  101. u32 bitPosInWord = pStrmData->bitPosInWord;
  102. out = ((u32)pStrm[0] << 24) | ((u32)pStrm[1] << 16) |
  103. ((u32)pStrm[2] << 8) | ((u32)pStrm[3]);
  104. if (bitPosInWord)
  105. {
  106. u32 byte = (u32)pStrm[4];
  107. u32 tmp = (8-bitPosInWord);
  108. out <<= bitPosInWord;
  109. out |= byte>>tmp;
  110. }
  111. return (out);
  112. }
  113. /* at least one bit in the buffer */
  114. else if (bits > 0)
  115. {
  116. shift = (i32)(24 + pStrmData->bitPosInWord);
  117. out = (u32)(*pStrm++) << shift;
  118. bits -= (i32)(8 - pStrmData->bitPosInWord);
  119. while (bits > 0)
  120. {
  121. shift -= 8;
  122. out |= (u32)(*pStrm++) << shift;
  123. bits -= 8;
  124. }
  125. return (out);
  126. }
  127. else
  128. return (0);
  129. }
  130. /*------------------------------------------------------------------------------
  131. Function: h264bsdFlushBits
  132. Functional description:
  133. Remove bits from the stream buffer
  134. Input:
  135. pStrmData pointer to stream data structure
  136. numBits number of bits to remove
  137. Output:
  138. none
  139. Returns:
  140. HANTRO_OK success
  141. END_OF_STREAM not enough bits left
  142. ------------------------------------------------------------------------------*/
  143. #ifndef H264DEC_NEON
  144. u32 h264bsdFlushBits(strmData_t *pStrmData, u32 numBits)
  145. {
  146. ASSERT(pStrmData);
  147. ASSERT(pStrmData->pStrmBuffStart);
  148. ASSERT(pStrmData->pStrmCurrPos);
  149. ASSERT(pStrmData->bitPosInWord < 8);
  150. ASSERT(pStrmData->bitPosInWord == (pStrmData->strmBuffReadBits & 0x7));
  151. pStrmData->strmBuffReadBits += numBits;
  152. pStrmData->bitPosInWord = pStrmData->strmBuffReadBits & 0x7;
  153. if ( (pStrmData->strmBuffReadBits ) <= (8*pStrmData->strmBuffSize) )
  154. {
  155. pStrmData->pStrmCurrPos = pStrmData->pStrmBuffStart +
  156. (pStrmData->strmBuffReadBits >> 3);
  157. return(HANTRO_OK);
  158. }
  159. else
  160. return(END_OF_STREAM);
  161. }
  162. #endif
  163. /*------------------------------------------------------------------------------
  164. Function: h264bsdIsByteAligned
  165. Functional description:
  166. Check if current stream position is byte aligned.
  167. Inputs:
  168. pStrmData pointer to stream data structure
  169. Outputs:
  170. none
  171. Returns:
  172. TRUE stream is byte aligned
  173. FALSE stream is not byte aligned
  174. ------------------------------------------------------------------------------*/
  175. u32 h264bsdIsByteAligned(strmData_t *pStrmData)
  176. {
  177. /* Variables */
  178. /* Code */
  179. if (!pStrmData->bitPosInWord)
  180. return(HANTRO_TRUE);
  181. else
  182. return(HANTRO_FALSE);
  183. }