/H264Dec/source/h264bsd_pic_param_set.c

http://github.com/mbebenita/Broadway · C · 335 lines · 205 code · 38 blank · 92 comment · 83 complexity · 28d1030831d9702852446d2893de73a2 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. h264bsdDecodePicParamSet
  24. ------------------------------------------------------------------------------*/
  25. /*------------------------------------------------------------------------------
  26. 1. Include headers
  27. ------------------------------------------------------------------------------*/
  28. #include "h264bsd_pic_param_set.h"
  29. #include "h264bsd_util.h"
  30. #include "h264bsd_vlc.h"
  31. #include "h264bsd_cfg.h"
  32. /*------------------------------------------------------------------------------
  33. 2. External compiler flags
  34. --------------------------------------------------------------------------------
  35. --------------------------------------------------------------------------------
  36. 3. Module defines
  37. ------------------------------------------------------------------------------*/
  38. /* lookup table for ceil(log2(numSliceGroups)), i.e. number of bits needed to
  39. * represent range [0, numSliceGroups)
  40. *
  41. * NOTE: if MAX_NUM_SLICE_GROUPS is higher than 8 this table has to be resized
  42. * accordingly */
  43. static const u32 CeilLog2NumSliceGroups[8] = {1, 1, 2, 2, 3, 3, 3, 3};
  44. /*------------------------------------------------------------------------------
  45. 4. Local function prototypes
  46. ------------------------------------------------------------------------------*/
  47. /*------------------------------------------------------------------------------
  48. Function name: h264bsdDecodePicParamSet
  49. Functional description:
  50. Decode picture parameter set information from the stream.
  51. Function allocates memory for
  52. - run lengths if slice group map type is 0
  53. - top-left and bottom-right arrays if map type is 2
  54. - for slice group ids if map type is 6
  55. Validity of some of the slice group mapping information depends
  56. on the image dimensions which are not known here. Therefore the
  57. validity has to be checked afterwards, currently in the parameter
  58. set activation phase.
  59. Inputs:
  60. pStrmData pointer to stream data structure
  61. Outputs:
  62. pPicParamSet decoded information is stored here
  63. Returns:
  64. HANTRO_OK success
  65. HANTRO_NOK failure, invalid information or end of stream
  66. MEMORY_ALLOCATION_ERROR for memory allocation failure
  67. ------------------------------------------------------------------------------*/
  68. u32 h264bsdDecodePicParamSet(strmData_t *pStrmData, picParamSet_t *pPicParamSet)
  69. {
  70. /* Variables */
  71. u32 tmp, i, value;
  72. i32 itmp;
  73. /* Code */
  74. ASSERT(pStrmData);
  75. ASSERT(pPicParamSet);
  76. H264SwDecMemset(pPicParamSet, 0, sizeof(picParamSet_t));
  77. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  78. &pPicParamSet->picParameterSetId);
  79. if (tmp != HANTRO_OK)
  80. return(tmp);
  81. if (pPicParamSet->picParameterSetId >= MAX_NUM_PIC_PARAM_SETS)
  82. {
  83. EPRINT("pic_parameter_set_id");
  84. return(HANTRO_NOK);
  85. }
  86. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  87. &pPicParamSet->seqParameterSetId);
  88. if (tmp != HANTRO_OK)
  89. return(tmp);
  90. if (pPicParamSet->seqParameterSetId >= MAX_NUM_SEQ_PARAM_SETS)
  91. {
  92. EPRINT("seq_param_set_id");
  93. return(HANTRO_NOK);
  94. }
  95. /* entropy_coding_mode_flag, shall be 0 for baseline profile */
  96. tmp = h264bsdGetBits(pStrmData, 1);
  97. if (tmp)
  98. {
  99. EPRINT("entropy_coding_mode_flag");
  100. return(HANTRO_NOK);
  101. }
  102. tmp = h264bsdGetBits(pStrmData, 1);
  103. if (tmp == END_OF_STREAM)
  104. return(HANTRO_NOK);
  105. pPicParamSet->picOrderPresentFlag = (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  106. /* num_slice_groups_minus1 */
  107. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  108. if (tmp != HANTRO_OK)
  109. return(tmp);
  110. pPicParamSet->numSliceGroups = value + 1;
  111. if (pPicParamSet->numSliceGroups > MAX_NUM_SLICE_GROUPS)
  112. {
  113. EPRINT("num_slice_groups_minus1");
  114. return(HANTRO_NOK);
  115. }
  116. /* decode slice group mapping information if more than one slice groups */
  117. if (pPicParamSet->numSliceGroups > 1)
  118. {
  119. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  120. &pPicParamSet->sliceGroupMapType);
  121. if (tmp != HANTRO_OK)
  122. return(tmp);
  123. if (pPicParamSet->sliceGroupMapType > 6)
  124. {
  125. EPRINT("slice_group_map_type");
  126. return(HANTRO_NOK);
  127. }
  128. if (pPicParamSet->sliceGroupMapType == 0)
  129. {
  130. ALLOCATE(pPicParamSet->runLength,
  131. pPicParamSet->numSliceGroups, u32);
  132. if (pPicParamSet->runLength == NULL)
  133. return(MEMORY_ALLOCATION_ERROR);
  134. for (i = 0; i < pPicParamSet->numSliceGroups; i++)
  135. {
  136. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  137. if (tmp != HANTRO_OK)
  138. return(tmp);
  139. pPicParamSet->runLength[i] = value+1;
  140. /* param values checked in CheckPps() */
  141. }
  142. }
  143. else if (pPicParamSet->sliceGroupMapType == 2)
  144. {
  145. ALLOCATE(pPicParamSet->topLeft,
  146. pPicParamSet->numSliceGroups - 1, u32);
  147. ALLOCATE(pPicParamSet->bottomRight,
  148. pPicParamSet->numSliceGroups - 1, u32);
  149. if (pPicParamSet->topLeft == NULL ||
  150. pPicParamSet->bottomRight == NULL)
  151. return(MEMORY_ALLOCATION_ERROR);
  152. for (i = 0; i < pPicParamSet->numSliceGroups - 1; i++)
  153. {
  154. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  155. if (tmp != HANTRO_OK)
  156. return(tmp);
  157. pPicParamSet->topLeft[i] = value;
  158. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  159. if (tmp != HANTRO_OK)
  160. return(tmp);
  161. pPicParamSet->bottomRight[i] = value;
  162. /* param values checked in CheckPps() */
  163. }
  164. }
  165. else if ( (pPicParamSet->sliceGroupMapType == 3) ||
  166. (pPicParamSet->sliceGroupMapType == 4) ||
  167. (pPicParamSet->sliceGroupMapType == 5) )
  168. {
  169. tmp = h264bsdGetBits(pStrmData, 1);
  170. if (tmp == END_OF_STREAM)
  171. return(HANTRO_NOK);
  172. pPicParamSet->sliceGroupChangeDirectionFlag =
  173. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  174. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  175. if (tmp != HANTRO_OK)
  176. return(tmp);
  177. pPicParamSet->sliceGroupChangeRate = value + 1;
  178. /* param value checked in CheckPps() */
  179. }
  180. else if (pPicParamSet->sliceGroupMapType == 6)
  181. {
  182. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  183. if (tmp != HANTRO_OK)
  184. return(tmp);
  185. pPicParamSet->picSizeInMapUnits = value + 1;
  186. ALLOCATE(pPicParamSet->sliceGroupId,
  187. pPicParamSet->picSizeInMapUnits, u32);
  188. if (pPicParamSet->sliceGroupId == NULL)
  189. return(MEMORY_ALLOCATION_ERROR);
  190. /* determine number of bits needed to represent range
  191. * [0, numSliceGroups) */
  192. tmp = CeilLog2NumSliceGroups[pPicParamSet->numSliceGroups-1];
  193. for (i = 0; i < pPicParamSet->picSizeInMapUnits; i++)
  194. {
  195. pPicParamSet->sliceGroupId[i] = h264bsdGetBits(pStrmData, tmp);
  196. if ( pPicParamSet->sliceGroupId[i] >=
  197. pPicParamSet->numSliceGroups )
  198. {
  199. EPRINT("slice_group_id");
  200. return(HANTRO_NOK);
  201. }
  202. }
  203. }
  204. }
  205. /* num_ref_idx_l0_active_minus1 */
  206. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  207. if (tmp != HANTRO_OK)
  208. return(tmp);
  209. if (value > 31)
  210. {
  211. EPRINT("num_ref_idx_l0_active_minus1");
  212. return(HANTRO_NOK);
  213. }
  214. pPicParamSet->numRefIdxL0Active = value + 1;
  215. /* num_ref_idx_l1_active_minus1 */
  216. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  217. if (tmp != HANTRO_OK)
  218. return(tmp);
  219. if (value > 31)
  220. {
  221. EPRINT("num_ref_idx_l1_active_minus1");
  222. return(HANTRO_NOK);
  223. }
  224. /* weighted_pred_flag, this shall be 0 for baseline profile */
  225. tmp = h264bsdGetBits(pStrmData, 1);
  226. if (tmp)
  227. {
  228. EPRINT("weighted_pred_flag");
  229. return(HANTRO_NOK);
  230. }
  231. /* weighted_bipred_idc */
  232. tmp = h264bsdGetBits(pStrmData, 2);
  233. if (tmp > 2)
  234. {
  235. EPRINT("weighted_bipred_idc");
  236. return(HANTRO_NOK);
  237. }
  238. /* pic_init_qp_minus26 */
  239. tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
  240. if (tmp != HANTRO_OK)
  241. return(tmp);
  242. if ((itmp < -26) || (itmp > 25))
  243. {
  244. EPRINT("pic_init_qp_minus26");
  245. return(HANTRO_NOK);
  246. }
  247. pPicParamSet->picInitQp = (u32)(itmp + 26);
  248. /* pic_init_qs_minus26 */
  249. tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
  250. if (tmp != HANTRO_OK)
  251. return(tmp);
  252. if ((itmp < -26) || (itmp > 25))
  253. {
  254. EPRINT("pic_init_qs_minus26");
  255. return(HANTRO_NOK);
  256. }
  257. tmp = h264bsdDecodeExpGolombSigned(pStrmData, &itmp);
  258. if (tmp != HANTRO_OK)
  259. return(tmp);
  260. if ((itmp < -12) || (itmp > 12))
  261. {
  262. EPRINT("chroma_qp_index_offset");
  263. return(HANTRO_NOK);
  264. }
  265. pPicParamSet->chromaQpIndexOffset = itmp;
  266. tmp = h264bsdGetBits(pStrmData, 1);
  267. if (tmp == END_OF_STREAM)
  268. return(HANTRO_NOK);
  269. pPicParamSet->deblockingFilterControlPresentFlag =
  270. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  271. tmp = h264bsdGetBits(pStrmData, 1);
  272. if (tmp == END_OF_STREAM)
  273. return(HANTRO_NOK);
  274. pPicParamSet->constrainedIntraPredFlag = (tmp == 1) ?
  275. HANTRO_TRUE : HANTRO_FALSE;
  276. tmp = h264bsdGetBits(pStrmData, 1);
  277. if (tmp == END_OF_STREAM)
  278. return(HANTRO_NOK);
  279. pPicParamSet->redundantPicCntPresentFlag = (tmp == 1) ?
  280. HANTRO_TRUE : HANTRO_FALSE;
  281. tmp = h264bsdRbspTrailingBits(pStrmData);
  282. /* ignore possible errors in trailing bits of parameters sets */
  283. return(HANTRO_OK);
  284. }