PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/H264Dec/source/h264bsd_util.c

http://github.com/mbebenita/Broadway
C | 286 lines | 82 code | 51 blank | 153 comment | 15 complexity | dd5fc818d3a2fb25519e6d6b03f9ddb0 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. h264bsdCountLeadingZeros
  24. h264bsdRbspTrailingBits
  25. h264bsdMoreRbspData
  26. h264bsdNextMbAddress
  27. h264bsdSetCurrImageMbPointers
  28. ------------------------------------------------------------------------------*/
  29. /*------------------------------------------------------------------------------
  30. 1. Include headers
  31. ------------------------------------------------------------------------------*/
  32. #include "h264bsd_util.h"
  33. /*------------------------------------------------------------------------------
  34. 2. External compiler flags
  35. --------------------------------------------------------------------------------
  36. --------------------------------------------------------------------------------
  37. 3. Module defines
  38. ------------------------------------------------------------------------------*/
  39. /* look-up table for expected values of stuffing bits */
  40. static const u32 stuffingTable[8] = {0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80};
  41. /* look-up table for chroma quantization parameter as a function of luma QP */
  42. const u32 h264bsdQpC[52] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
  43. 20,21,22,23,24,25,26,27,28,29,29,30,31,32,32,33,34,34,35,35,36,36,37,37,37,
  44. 38,38,38,39,39,39,39};
  45. /*------------------------------------------------------------------------------
  46. 4. Local function prototypes
  47. ------------------------------------------------------------------------------*/
  48. /*------------------------------------------------------------------------------
  49. 5.1 Function: h264bsdCountLeadingZeros
  50. Functional description:
  51. Count leading zeros in a code word. Code word is assumed to be
  52. right-aligned, last bit of the code word in the lsb of the value.
  53. Inputs:
  54. value code word
  55. length number of bits in the code word
  56. Outputs:
  57. none
  58. Returns:
  59. number of leading zeros in the code word
  60. ------------------------------------------------------------------------------*/
  61. #ifndef H264DEC_NEON
  62. u32 h264bsdCountLeadingZeros(u32 value, u32 length)
  63. {
  64. /* Variables */
  65. u32 zeros = 0;
  66. u32 mask = 1 << (length - 1);
  67. /* Code */
  68. ASSERT(length <= 32);
  69. while (mask && !(value & mask))
  70. {
  71. zeros++;
  72. mask >>= 1;
  73. }
  74. return(zeros);
  75. }
  76. #endif
  77. /*------------------------------------------------------------------------------
  78. 5.2 Function: h264bsdRbspTrailingBits
  79. Functional description:
  80. Check Raw Byte Stream Payload (RBSP) trailing bits, i.e. stuffing.
  81. Rest of the current byte (whole byte if allready byte aligned)
  82. in the stream buffer shall contain a '1' bit followed by zero or
  83. more '0' bits.
  84. Inputs:
  85. pStrmData pointer to stream data structure
  86. Outputs:
  87. none
  88. Returns:
  89. HANTRO_OK RBSP trailing bits found
  90. HANTRO_NOK otherwise
  91. ------------------------------------------------------------------------------*/
  92. u32 h264bsdRbspTrailingBits(strmData_t *pStrmData)
  93. {
  94. /* Variables */
  95. u32 stuffing;
  96. u32 stuffingLength;
  97. /* Code */
  98. ASSERT(pStrmData);
  99. ASSERT(pStrmData->bitPosInWord < 8);
  100. stuffingLength = 8 - pStrmData->bitPosInWord;
  101. stuffing = h264bsdGetBits(pStrmData, stuffingLength);
  102. if (stuffing == END_OF_STREAM)
  103. return(HANTRO_NOK);
  104. if (stuffing != stuffingTable[stuffingLength - 1])
  105. return(HANTRO_NOK);
  106. else
  107. return(HANTRO_OK);
  108. }
  109. /*------------------------------------------------------------------------------
  110. 5.3 Function: h264bsdMoreRbspData
  111. Functional description:
  112. Check if there is more data in the current RBSP. The standard
  113. defines this function so that there is more data if
  114. -more than 8 bits left or
  115. -last bits are not RBSP trailing bits
  116. Inputs:
  117. pStrmData pointer to stream data structure
  118. Outputs:
  119. none
  120. Returns:
  121. HANTRO_TRUE there is more data
  122. HANTRO_FALSE no more data
  123. ------------------------------------------------------------------------------*/
  124. u32 h264bsdMoreRbspData(strmData_t *pStrmData)
  125. {
  126. /* Variables */
  127. u32 bits;
  128. /* Code */
  129. ASSERT(pStrmData);
  130. ASSERT(pStrmData->strmBuffReadBits <= 8 * pStrmData->strmBuffSize);
  131. bits = pStrmData->strmBuffSize * 8 - pStrmData->strmBuffReadBits;
  132. if (bits == 0)
  133. return(HANTRO_FALSE);
  134. if ( (bits > 8) ||
  135. ((h264bsdShowBits32(pStrmData)>>(32-bits)) != (1 << (bits-1))) )
  136. return(HANTRO_TRUE);
  137. else
  138. return(HANTRO_FALSE);
  139. }
  140. /*------------------------------------------------------------------------------
  141. 5.4 Function: h264bsdNextMbAddress
  142. Functional description:
  143. Get address of the next macroblock in the current slice group.
  144. Inputs:
  145. pSliceGroupMap slice group for each macroblock
  146. picSizeInMbs size of the picture
  147. currMbAddr where to start
  148. Outputs:
  149. none
  150. Returns:
  151. address of the next macroblock
  152. 0 if none of the following macroblocks belong to same slice
  153. group as currMbAddr
  154. ------------------------------------------------------------------------------*/
  155. u32 h264bsdNextMbAddress(u32 *pSliceGroupMap, u32 picSizeInMbs, u32 currMbAddr)
  156. {
  157. /* Variables */
  158. u32 i, sliceGroup, tmp;
  159. /* Code */
  160. ASSERT(pSliceGroupMap);
  161. ASSERT(picSizeInMbs);
  162. ASSERT(currMbAddr < picSizeInMbs);
  163. sliceGroup = pSliceGroupMap[currMbAddr];
  164. i = currMbAddr + 1;
  165. tmp = pSliceGroupMap[i];
  166. while ((i < picSizeInMbs) && (tmp != sliceGroup))
  167. {
  168. i++;
  169. tmp = pSliceGroupMap[i];
  170. }
  171. if (i == picSizeInMbs)
  172. i = 0;
  173. return(i);
  174. }
  175. /*------------------------------------------------------------------------------
  176. 5.5 Function: h264bsdSetCurrImageMbPointers
  177. Functional description:
  178. Set luma and chroma pointers in image_t for current MB
  179. Inputs:
  180. image Current image
  181. mbNum number of current MB
  182. Outputs:
  183. none
  184. Returns:
  185. none
  186. ------------------------------------------------------------------------------*/
  187. void h264bsdSetCurrImageMbPointers(image_t *image, u32 mbNum)
  188. {
  189. u32 width, height;
  190. u32 picSize;
  191. u32 row, col;
  192. u32 tmp;
  193. width = image->width;
  194. height = image->height;
  195. row = mbNum / width;
  196. col = mbNum % width;
  197. tmp = row * width;
  198. picSize = width * height;
  199. image->luma = (u8*)(image->data + col * 16 + tmp * 256);
  200. image->cb = (u8*)(image->data + picSize * 256 + tmp * 64 + col * 8);
  201. image->cr = (u8*)(image->cb + picSize * 64);
  202. }