PageRenderTime 36ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/H264Dec/source/h264bsd_slice_data.c

http://github.com/mbebenita/Broadway
C | 354 lines | 162 code | 52 blank | 140 comment | 30 complexity | 3dafa2e8dd475a053752471afe408199 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. h264bsdDecodeSliceData
  24. SetMbParams
  25. h264bsdMarkSliceCorrupted
  26. ------------------------------------------------------------------------------*/
  27. /*------------------------------------------------------------------------------
  28. 1. Include headers
  29. ------------------------------------------------------------------------------*/
  30. #include "h264bsd_slice_data.h"
  31. #include "h264bsd_util.h"
  32. #include "h264bsd_vlc.h"
  33. /*------------------------------------------------------------------------------
  34. 2. External compiler flags
  35. --------------------------------------------------------------------------------
  36. --------------------------------------------------------------------------------
  37. 3. Module defines
  38. ------------------------------------------------------------------------------*/
  39. /*------------------------------------------------------------------------------
  40. 4. Local function prototypes
  41. ------------------------------------------------------------------------------*/
  42. static void SetMbParams(mbStorage_t *pMb, sliceHeader_t *pSlice, u32 sliceId,
  43. i32 chromaQpIndexOffset);
  44. /*------------------------------------------------------------------------------
  45. 5.1 Function name: h264bsdDecodeSliceData
  46. Functional description:
  47. Decode one slice. Function decodes stream data, i.e. macroblocks
  48. and possible skip_run fields. h264bsdDecodeMacroblock function is
  49. called to handle all other macroblock related processing.
  50. Macroblock to slice group mapping is considered when next
  51. macroblock to process is determined (h264bsdNextMbAddress function)
  52. map
  53. Inputs:
  54. pStrmData pointer to stream data structure
  55. pStorage pointer to storage structure
  56. currImage pointer to current processed picture, needed for
  57. intra prediction of the macroblocks
  58. pSliceHeader pointer to slice header of the current slice
  59. Outputs:
  60. currImage processed macroblocks are written to current image
  61. pStorage mbStorage structure of each processed macroblock
  62. is updated here
  63. Returns:
  64. HANTRO_OK success
  65. HANTRO_NOK invalid stream data
  66. ------------------------------------------------------------------------------*/
  67. u32 h264bsdDecodeSliceData(strmData_t *pStrmData, storage_t *pStorage,
  68. image_t *currImage, sliceHeader_t *pSliceHeader)
  69. {
  70. /* Variables */
  71. u8 mbData[384 + 15 + 32];
  72. u8 *data;
  73. u32 tmp;
  74. u32 skipRun;
  75. u32 prevSkipped;
  76. u32 currMbAddr;
  77. u32 moreMbs;
  78. u32 mbCount;
  79. i32 qpY;
  80. macroblockLayer_t *mbLayer;
  81. /* Code */
  82. ASSERT(pStrmData);
  83. ASSERT(pSliceHeader);
  84. ASSERT(pStorage);
  85. ASSERT(pSliceHeader->firstMbInSlice < pStorage->picSizeInMbs);
  86. /* ensure 16-byte alignment */
  87. data = (u8*)ALIGN(mbData, 16);
  88. mbLayer = pStorage->mbLayer;
  89. currMbAddr = pSliceHeader->firstMbInSlice;
  90. skipRun = 0;
  91. prevSkipped = HANTRO_FALSE;
  92. /* increment slice index, will be one for decoding of the first slice of
  93. * the picture */
  94. pStorage->slice->sliceId++;
  95. /* lastMbAddr stores address of the macroblock that was last successfully
  96. * decoded, needed for error handling */
  97. pStorage->slice->lastMbAddr = 0;
  98. mbCount = 0;
  99. /* initial quantization parameter for the slice is obtained as the sum of
  100. * initial QP for the picture and sliceQpDelta for the current slice */
  101. qpY = (i32)pStorage->activePps->picInitQp + pSliceHeader->sliceQpDelta;
  102. do
  103. {
  104. /* primary picture and already decoded macroblock -> error */
  105. if (!pSliceHeader->redundantPicCnt && pStorage->mb[currMbAddr].decoded)
  106. {
  107. EPRINT("Primary and already decoded");
  108. return(HANTRO_NOK);
  109. }
  110. SetMbParams(pStorage->mb + currMbAddr, pSliceHeader,
  111. pStorage->slice->sliceId, pStorage->activePps->chromaQpIndexOffset);
  112. if (!IS_I_SLICE(pSliceHeader->sliceType))
  113. {
  114. if (!prevSkipped)
  115. {
  116. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &skipRun);
  117. if (tmp != HANTRO_OK)
  118. return(tmp);
  119. /* skip_run shall be less than or equal to number of
  120. * macroblocks left */
  121. if (skipRun > (pStorage->picSizeInMbs - currMbAddr))
  122. {
  123. EPRINT("skip_run");
  124. return(HANTRO_NOK);
  125. }
  126. if (skipRun)
  127. {
  128. prevSkipped = HANTRO_TRUE;
  129. H264SwDecMemset(&mbLayer->mbPred, 0, sizeof(mbPred_t));
  130. /* mark current macroblock skipped */
  131. mbLayer->mbType = P_Skip;
  132. }
  133. }
  134. }
  135. if (skipRun)
  136. {
  137. DEBUG(("Skipping macroblock %d\n", currMbAddr));
  138. skipRun--;
  139. }
  140. else
  141. {
  142. prevSkipped = HANTRO_FALSE;
  143. tmp = h264bsdDecodeMacroblockLayer(pStrmData, mbLayer,
  144. pStorage->mb + currMbAddr, pSliceHeader->sliceType,
  145. pSliceHeader->numRefIdxL0Active);
  146. if (tmp != HANTRO_OK)
  147. {
  148. EPRINT("macroblock_layer");
  149. return(tmp);
  150. }
  151. }
  152. tmp = h264bsdDecodeMacroblock(pStorage->mb + currMbAddr, mbLayer,
  153. currImage, pStorage->dpb, &qpY, currMbAddr,
  154. pStorage->activePps->constrainedIntraPredFlag, data);
  155. if (tmp != HANTRO_OK)
  156. {
  157. EPRINT("MACRO_BLOCK");
  158. return(tmp);
  159. }
  160. /* increment macroblock count only for macroblocks that were decoded
  161. * for the first time (redundant slices) */
  162. if (pStorage->mb[currMbAddr].decoded == 1)
  163. mbCount++;
  164. /* keep on processing as long as there is stream data left or
  165. * processing of macroblocks to be skipped based on the last skipRun is
  166. * not finished */
  167. moreMbs = (h264bsdMoreRbspData(pStrmData) || skipRun) ?
  168. HANTRO_TRUE : HANTRO_FALSE;
  169. /* lastMbAddr is only updated for intra slices (all macroblocks of
  170. * inter slices will be lost in case of an error) */
  171. if (IS_I_SLICE(pSliceHeader->sliceType))
  172. pStorage->slice->lastMbAddr = currMbAddr;
  173. currMbAddr = h264bsdNextMbAddress(pStorage->sliceGroupMap,
  174. pStorage->picSizeInMbs, currMbAddr);
  175. /* data left in the buffer but no more macroblocks for current slice
  176. * group -> error */
  177. if (moreMbs && !currMbAddr)
  178. {
  179. EPRINT("Next mb address");
  180. return(HANTRO_NOK);
  181. }
  182. } while (moreMbs);
  183. if ((pStorage->slice->numDecodedMbs + mbCount) > pStorage->picSizeInMbs)
  184. {
  185. EPRINT("Num decoded mbs");
  186. return(HANTRO_NOK);
  187. }
  188. pStorage->slice->numDecodedMbs += mbCount;
  189. return(HANTRO_OK);
  190. }
  191. /*------------------------------------------------------------------------------
  192. 5.2 Function: SetMbParams
  193. Functional description:
  194. Set macroblock parameters that remain constant for this slice
  195. Inputs:
  196. pSlice pointer to current slice header
  197. sliceId id of the current slice
  198. chromaQpIndexOffset
  199. Outputs:
  200. pMb pointer to macroblock structure which is updated
  201. Returns:
  202. none
  203. ------------------------------------------------------------------------------*/
  204. void SetMbParams(mbStorage_t *pMb, sliceHeader_t *pSlice, u32 sliceId,
  205. i32 chromaQpIndexOffset)
  206. {
  207. /* Variables */
  208. u32 tmp1;
  209. i32 tmp2, tmp3;
  210. /* Code */
  211. tmp1 = pSlice->disableDeblockingFilterIdc;
  212. tmp2 = pSlice->sliceAlphaC0Offset;
  213. tmp3 = pSlice->sliceBetaOffset;
  214. pMb->sliceId = sliceId;
  215. pMb->disableDeblockingFilterIdc = tmp1;
  216. pMb->filterOffsetA = tmp2;
  217. pMb->filterOffsetB = tmp3;
  218. pMb->chromaQpIndexOffset = chromaQpIndexOffset;
  219. }
  220. /*------------------------------------------------------------------------------
  221. 5.3 Function name: h264bsdMarkSliceCorrupted
  222. Functional description:
  223. Mark macroblocks of the slice corrupted. If lastMbAddr in the slice
  224. storage is set -> picWidhtInMbs (or at least 10) macroblocks back
  225. from the lastMbAddr are marked corrupted. However, if lastMbAddr
  226. is not set -> all macroblocks of the slice are marked.
  227. Inputs:
  228. pStorage pointer to storage structure
  229. firstMbInSlice address of the first macroblock in the slice, this
  230. identifies the slice to be marked corrupted
  231. Outputs:
  232. pStorage mbStorage for the corrupted macroblocks updated
  233. Returns:
  234. none
  235. ------------------------------------------------------------------------------*/
  236. void h264bsdMarkSliceCorrupted(storage_t *pStorage, u32 firstMbInSlice)
  237. {
  238. /* Variables */
  239. u32 tmp, i;
  240. u32 sliceId;
  241. u32 currMbAddr;
  242. /* Code */
  243. ASSERT(pStorage);
  244. ASSERT(firstMbInSlice < pStorage->picSizeInMbs);
  245. currMbAddr = firstMbInSlice;
  246. sliceId = pStorage->slice->sliceId;
  247. /* DecodeSliceData sets lastMbAddr for I slices -> if it was set, go back
  248. * MAX(picWidthInMbs, 10) macroblocks and start marking from there */
  249. if (pStorage->slice->lastMbAddr)
  250. {
  251. ASSERT(pStorage->mb[pStorage->slice->lastMbAddr].sliceId == sliceId);
  252. i = pStorage->slice->lastMbAddr - 1;
  253. tmp = 0;
  254. while (i > currMbAddr)
  255. {
  256. if (pStorage->mb[i].sliceId == sliceId)
  257. {
  258. tmp++;
  259. if (tmp >= MAX(pStorage->activeSps->picWidthInMbs, 10))
  260. break;
  261. }
  262. i--;
  263. }
  264. currMbAddr = i;
  265. }
  266. do
  267. {
  268. if ( (pStorage->mb[currMbAddr].sliceId == sliceId) &&
  269. (pStorage->mb[currMbAddr].decoded) )
  270. {
  271. pStorage->mb[currMbAddr].decoded--;
  272. }
  273. else
  274. {
  275. break;
  276. }
  277. currMbAddr = h264bsdNextMbAddress(pStorage->sliceGroupMap,
  278. pStorage->picSizeInMbs, currMbAddr);
  279. } while (currMbAddr);
  280. }