PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/media/libstagefright/codecs/on2/h264dec/source/h264bsd_pic_order_cnt.c

https://bitbucket.org/androidarmv6/android_frameworks_av
C | 347 lines | 180 code | 44 blank | 123 comment | 43 complexity | cb85068194756d772f1d35ec4394f356 MD5 | raw file
  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. h264bsdDecodePicOrderCnt
  24. ------------------------------------------------------------------------------*/
  25. /*------------------------------------------------------------------------------
  26. 1. Include headers
  27. ------------------------------------------------------------------------------*/
  28. #include "h264bsd_util.h"
  29. #include "h264bsd_pic_order_cnt.h"
  30. /*------------------------------------------------------------------------------
  31. 2. External compiler flags
  32. --------------------------------------------------------------------------------
  33. --------------------------------------------------------------------------------
  34. 3. Module defines
  35. ------------------------------------------------------------------------------*/
  36. /*------------------------------------------------------------------------------
  37. 4. Local function prototypes
  38. ------------------------------------------------------------------------------*/
  39. /*------------------------------------------------------------------------------
  40. Function: h264bsdDecodePicOrderCnt
  41. Functional description:
  42. Compute picture order count for a picture. Function implements
  43. computation of all POC types (0, 1 and 2), type is obtained from
  44. sps. See standard for description of the POC types and how POC is
  45. computed for each type.
  46. Function returns the minimum of top field and bottom field pic
  47. order counts.
  48. Inputs:
  49. poc pointer to previous results
  50. sps pointer to sequence parameter set
  51. slicHeader pointer to current slice header, frame number and
  52. other params needed for POC computation
  53. pNalUnit pointer to current NAL unit structrue, function needs
  54. to know if this is an IDR picture and also if this is
  55. a reference picture
  56. Outputs:
  57. poc results stored here for computation of next POC
  58. Returns:
  59. picture order count
  60. ------------------------------------------------------------------------------*/
  61. i32 h264bsdDecodePicOrderCnt(pocStorage_t *poc, seqParamSet_t *sps,
  62. sliceHeader_t *pSliceHeader, nalUnit_t *pNalUnit)
  63. {
  64. /* Variables */
  65. u32 i;
  66. i32 picOrderCnt;
  67. u32 frameNumOffset, absFrameNum, picOrderCntCycleCnt;
  68. u32 frameNumInPicOrderCntCycle;
  69. i32 expectedDeltaPicOrderCntCycle;
  70. u32 containsMmco5;
  71. /* Code */
  72. ASSERT(poc);
  73. ASSERT(sps);
  74. ASSERT(pSliceHeader);
  75. ASSERT(pNalUnit);
  76. ASSERT(sps->picOrderCntType <= 2);
  77. #if 0
  78. /* JanSa: I don't think this is necessary, don't see any reason to
  79. * increment prevFrameNum one by one instead of one big increment.
  80. * However, standard specifies that this should be done -> if someone
  81. * figures out any case when the outcome would be different for step by
  82. * step increment, this part of the code should be enabled */
  83. /* if there was a gap in frame numbering and picOrderCntType is 1 or 2 ->
  84. * "compute" pic order counts for non-existing frames. These are not
  85. * actually computed, but process needs to be done to update the
  86. * prevFrameNum and prevFrameNumOffset */
  87. if ( sps->picOrderCntType > 0 &&
  88. pSliceHeader->frameNum != poc->prevFrameNum &&
  89. pSliceHeader->frameNum != ((poc->prevFrameNum + 1) % sps->maxFrameNum))
  90. {
  91. /* use variable i for unUsedShortTermFrameNum */
  92. i = (poc->prevFrameNum + 1) % sps->maxFrameNum;
  93. do
  94. {
  95. if (poc->prevFrameNum > i)
  96. frameNumOffset = poc->prevFrameNumOffset + sps->maxFrameNum;
  97. else
  98. frameNumOffset = poc->prevFrameNumOffset;
  99. poc->prevFrameNumOffset = frameNumOffset;
  100. poc->prevFrameNum = i;
  101. i = (i + 1) % sps->maxFrameNum;
  102. } while (i != pSliceHeader->frameNum);
  103. }
  104. #endif
  105. /* check if current slice includes mmco equal to 5 */
  106. containsMmco5 = HANTRO_FALSE;
  107. if (pSliceHeader->decRefPicMarking.adaptiveRefPicMarkingModeFlag)
  108. {
  109. i = 0;
  110. while (pSliceHeader->decRefPicMarking.operation[i].
  111. memoryManagementControlOperation)
  112. {
  113. if (pSliceHeader->decRefPicMarking.operation[i].
  114. memoryManagementControlOperation == 5)
  115. {
  116. containsMmco5 = HANTRO_TRUE;
  117. break;
  118. }
  119. i++;
  120. }
  121. }
  122. switch (sps->picOrderCntType)
  123. {
  124. case 0:
  125. /* set prevPicOrderCnt values for IDR frame */
  126. if (IS_IDR_NAL_UNIT(pNalUnit))
  127. {
  128. poc->prevPicOrderCntMsb = 0;
  129. poc->prevPicOrderCntLsb = 0;
  130. }
  131. /* compute picOrderCntMsb (stored in picOrderCnt variable) */
  132. if ( (pSliceHeader->picOrderCntLsb < poc->prevPicOrderCntLsb) &&
  133. ((poc->prevPicOrderCntLsb - pSliceHeader->picOrderCntLsb) >=
  134. sps->maxPicOrderCntLsb/2) )
  135. {
  136. picOrderCnt = poc->prevPicOrderCntMsb +
  137. (i32)sps->maxPicOrderCntLsb;
  138. }
  139. else if ((pSliceHeader->picOrderCntLsb > poc->prevPicOrderCntLsb) &&
  140. ((pSliceHeader->picOrderCntLsb - poc->prevPicOrderCntLsb) >
  141. sps->maxPicOrderCntLsb/2) )
  142. {
  143. picOrderCnt = poc->prevPicOrderCntMsb -
  144. (i32)sps->maxPicOrderCntLsb;
  145. }
  146. else
  147. picOrderCnt = poc->prevPicOrderCntMsb;
  148. /* standard specifies that prevPicOrderCntMsb is from previous
  149. * rererence frame -> replace old value only if current frame is
  150. * rererence frame */
  151. if (pNalUnit->nalRefIdc)
  152. poc->prevPicOrderCntMsb = picOrderCnt;
  153. /* compute top field order cnt (stored in picOrderCnt) */
  154. picOrderCnt += (i32)pSliceHeader->picOrderCntLsb;
  155. /* if delta for bottom field is negative -> bottom will be the
  156. * minimum pic order count */
  157. if (pSliceHeader->deltaPicOrderCntBottom < 0)
  158. picOrderCnt += pSliceHeader->deltaPicOrderCntBottom;
  159. /* standard specifies that prevPicOrderCntLsb is from previous
  160. * rererence frame -> replace old value only if current frame is
  161. * rererence frame */
  162. if (pNalUnit->nalRefIdc)
  163. {
  164. /* if current frame contains mmco5 -> modify values to be
  165. * stored */
  166. if (containsMmco5)
  167. {
  168. poc->prevPicOrderCntMsb = 0;
  169. /* prevPicOrderCntLsb should be the top field picOrderCnt
  170. * if previous frame included mmco5. Top field picOrderCnt
  171. * for frames containing mmco5 is obtained by subtracting
  172. * the picOrderCnt from original top field order count ->
  173. * value is zero if top field was the minimum, i.e. delta
  174. * for bottom was positive, otherwise value is
  175. * -deltaPicOrderCntBottom */
  176. if (pSliceHeader->deltaPicOrderCntBottom < 0)
  177. poc->prevPicOrderCntLsb =
  178. (u32)(-pSliceHeader->deltaPicOrderCntBottom);
  179. else
  180. poc->prevPicOrderCntLsb = 0;
  181. picOrderCnt = 0;
  182. }
  183. else
  184. {
  185. poc->prevPicOrderCntLsb = pSliceHeader->picOrderCntLsb;
  186. }
  187. }
  188. break;
  189. case 1:
  190. /* step 1 (in the description in the standard) */
  191. if (IS_IDR_NAL_UNIT(pNalUnit))
  192. frameNumOffset = 0;
  193. else if (poc->prevFrameNum > pSliceHeader->frameNum)
  194. frameNumOffset = poc->prevFrameNumOffset + sps->maxFrameNum;
  195. else
  196. frameNumOffset = poc->prevFrameNumOffset;
  197. /* step 2 */
  198. if (sps->numRefFramesInPicOrderCntCycle)
  199. absFrameNum = frameNumOffset + pSliceHeader->frameNum;
  200. else
  201. absFrameNum = 0;
  202. if (pNalUnit->nalRefIdc == 0 && absFrameNum > 0)
  203. absFrameNum -= 1;
  204. /* step 3 */
  205. if (absFrameNum > 0)
  206. {
  207. picOrderCntCycleCnt =
  208. (absFrameNum - 1)/sps->numRefFramesInPicOrderCntCycle;
  209. frameNumInPicOrderCntCycle =
  210. (absFrameNum - 1)%sps->numRefFramesInPicOrderCntCycle;
  211. }
  212. /* step 4 */
  213. expectedDeltaPicOrderCntCycle = 0;
  214. for (i = 0; i < sps->numRefFramesInPicOrderCntCycle; i++)
  215. expectedDeltaPicOrderCntCycle += sps->offsetForRefFrame[i];
  216. /* step 5 (picOrderCnt used to store expectedPicOrderCnt) */
  217. /*lint -esym(644,picOrderCntCycleCnt) always initialized */
  218. /*lint -esym(644,frameNumInPicOrderCntCycle) always initialized */
  219. if (absFrameNum > 0)
  220. {
  221. picOrderCnt =
  222. (i32)picOrderCntCycleCnt * expectedDeltaPicOrderCntCycle;
  223. for (i = 0; i <= frameNumInPicOrderCntCycle; i++)
  224. picOrderCnt += sps->offsetForRefFrame[i];
  225. }
  226. else
  227. picOrderCnt = 0;
  228. if (pNalUnit->nalRefIdc == 0)
  229. picOrderCnt += sps->offsetForNonRefPic;
  230. /* step 6 (picOrderCnt is top field order cnt if delta for bottom
  231. * is positive, otherwise it is bottom field order cnt) */
  232. picOrderCnt += pSliceHeader->deltaPicOrderCnt[0];
  233. if ( (sps->offsetForTopToBottomField +
  234. pSliceHeader->deltaPicOrderCnt[1]) < 0 )
  235. {
  236. picOrderCnt += sps->offsetForTopToBottomField +
  237. pSliceHeader->deltaPicOrderCnt[1];
  238. }
  239. /* if current picture contains mmco5 -> set prevFrameNumOffset and
  240. * prevFrameNum to 0 for computation of picOrderCnt of next
  241. * frame, otherwise store frameNum and frameNumOffset to poc
  242. * structure */
  243. if (!containsMmco5)
  244. {
  245. poc->prevFrameNumOffset = frameNumOffset;
  246. poc->prevFrameNum = pSliceHeader->frameNum;
  247. }
  248. else
  249. {
  250. poc->prevFrameNumOffset = 0;
  251. poc->prevFrameNum = 0;
  252. picOrderCnt = 0;
  253. }
  254. break;
  255. default: /* case 2 */
  256. /* derive frameNumOffset */
  257. if (IS_IDR_NAL_UNIT(pNalUnit))
  258. frameNumOffset = 0;
  259. else if (poc->prevFrameNum > pSliceHeader->frameNum)
  260. frameNumOffset = poc->prevFrameNumOffset + sps->maxFrameNum;
  261. else
  262. frameNumOffset = poc->prevFrameNumOffset;
  263. /* derive picOrderCnt (type 2 has same value for top and bottom
  264. * field order cnts) */
  265. if (IS_IDR_NAL_UNIT(pNalUnit))
  266. picOrderCnt = 0;
  267. else if (pNalUnit->nalRefIdc == 0)
  268. picOrderCnt =
  269. 2 * (i32)(frameNumOffset + pSliceHeader->frameNum) - 1;
  270. else
  271. picOrderCnt =
  272. 2 * (i32)(frameNumOffset + pSliceHeader->frameNum);
  273. /* if current picture contains mmco5 -> set prevFrameNumOffset and
  274. * prevFrameNum to 0 for computation of picOrderCnt of next
  275. * frame, otherwise store frameNum and frameNumOffset to poc
  276. * structure */
  277. if (!containsMmco5)
  278. {
  279. poc->prevFrameNumOffset = frameNumOffset;
  280. poc->prevFrameNum = pSliceHeader->frameNum;
  281. }
  282. else
  283. {
  284. poc->prevFrameNumOffset = 0;
  285. poc->prevFrameNum = 0;
  286. picOrderCnt = 0;
  287. }
  288. break;
  289. }
  290. /*lint -esym(644,picOrderCnt) always initialized */
  291. return(picOrderCnt);
  292. }