/H264Dec/source/h264bsd_seq_param_set.c

http://github.com/mbebenita/Broadway · C · 577 lines · 356 code · 80 blank · 141 comment · 113 complexity · 312229a06c188cee002d58b1d0313b38 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. h264bsdDecodeSeqParamSet
  24. GetDpbSize
  25. h264bsdCompareSeqParamSets
  26. ------------------------------------------------------------------------------*/
  27. /*------------------------------------------------------------------------------
  28. 1. Include headers
  29. ------------------------------------------------------------------------------*/
  30. #include "h264bsd_seq_param_set.h"
  31. #include "h264bsd_util.h"
  32. #include "h264bsd_vlc.h"
  33. #include "h264bsd_vui.h"
  34. #include "h264bsd_cfg.h"
  35. /*------------------------------------------------------------------------------
  36. 2. External compiler flags
  37. --------------------------------------------------------------------------------
  38. --------------------------------------------------------------------------------
  39. 3. Module defines
  40. ------------------------------------------------------------------------------*/
  41. /* enumeration to indicate invalid return value from the GetDpbSize function */
  42. enum {INVALID_DPB_SIZE = 0x7FFFFFFF};
  43. /*------------------------------------------------------------------------------
  44. 4. Local function prototypes
  45. ------------------------------------------------------------------------------*/
  46. static u32 GetDpbSize(u32 picSizeInMbs, u32 levelIdc);
  47. /*------------------------------------------------------------------------------
  48. Function name: h264bsdDecodeSeqParamSet
  49. Functional description:
  50. Decode sequence parameter set information from the stream.
  51. Function allocates memory for offsetForRefFrame array if
  52. picture order count type is 1 and numRefFramesInPicOrderCntCycle
  53. is greater than zero.
  54. Inputs:
  55. pStrmData pointer to stream data structure
  56. Outputs:
  57. pSeqParamSet decoded information is stored here
  58. Returns:
  59. HANTRO_OK success
  60. HANTRO_NOK failure, invalid information or end of stream
  61. MEMORY_ALLOCATION_ERROR for memory allocation failure
  62. ------------------------------------------------------------------------------*/
  63. u32 h264bsdDecodeSeqParamSet(strmData_t *pStrmData, seqParamSet_t *pSeqParamSet)
  64. {
  65. /* Variables */
  66. u32 tmp, i, value;
  67. /* Code */
  68. ASSERT(pStrmData);
  69. ASSERT(pSeqParamSet);
  70. H264SwDecMemset(pSeqParamSet, 0, sizeof(seqParamSet_t));
  71. /* profile_idc */
  72. tmp = h264bsdGetBits(pStrmData, 8);
  73. if (tmp == END_OF_STREAM)
  74. return(HANTRO_NOK);
  75. if (tmp != 66)
  76. {
  77. DEBUG(("NOT BASELINE PROFILE %d\n", tmp));
  78. }
  79. pSeqParamSet->profileIdc = tmp;
  80. /* constrained_set0_flag */
  81. tmp = h264bsdGetBits(pStrmData, 1);
  82. /* constrained_set1_flag */
  83. tmp = h264bsdGetBits(pStrmData, 1);
  84. /* constrained_set2_flag */
  85. tmp = h264bsdGetBits(pStrmData, 1);
  86. if (tmp == END_OF_STREAM)
  87. return(HANTRO_NOK);
  88. /* reserved_zero_5bits, values of these bits shall be ignored */
  89. tmp = h264bsdGetBits(pStrmData, 5);
  90. if (tmp == END_OF_STREAM)
  91. return(HANTRO_NOK);
  92. tmp = h264bsdGetBits(pStrmData, 8);
  93. if (tmp == END_OF_STREAM)
  94. return(HANTRO_NOK);
  95. pSeqParamSet->levelIdc = tmp;
  96. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  97. &pSeqParamSet->seqParameterSetId);
  98. if (tmp != HANTRO_OK)
  99. return(tmp);
  100. if (pSeqParamSet->seqParameterSetId >= MAX_NUM_SEQ_PARAM_SETS)
  101. {
  102. EPRINT("seq_param_set_id");
  103. return(HANTRO_NOK);
  104. }
  105. /* log2_max_frame_num_minus4 */
  106. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  107. if (tmp != HANTRO_OK)
  108. return(tmp);
  109. if (value > 12)
  110. {
  111. EPRINT("log2_max_frame_num_minus4");
  112. return(HANTRO_NOK);
  113. }
  114. /* maxFrameNum = 2^(log2_max_frame_num_minus4 + 4) */
  115. pSeqParamSet->maxFrameNum = 1 << (value+4);
  116. /* valid POC types are 0, 1 and 2 */
  117. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  118. if (tmp != HANTRO_OK)
  119. return(tmp);
  120. if (value > 2)
  121. {
  122. EPRINT("pic_order_cnt_type");
  123. return(HANTRO_NOK);
  124. }
  125. pSeqParamSet->picOrderCntType = value;
  126. if (pSeqParamSet->picOrderCntType == 0)
  127. {
  128. /* log2_max_pic_order_cnt_lsb_minus4 */
  129. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  130. if (tmp != HANTRO_OK)
  131. return(tmp);
  132. if (value > 12)
  133. {
  134. EPRINT("log2_max_pic_order_cnt_lsb_minus4");
  135. return(HANTRO_NOK);
  136. }
  137. /* maxPicOrderCntLsb = 2^(log2_max_pic_order_cnt_lsb_minus4 + 4) */
  138. pSeqParamSet->maxPicOrderCntLsb = 1 << (value+4);
  139. }
  140. else if (pSeqParamSet->picOrderCntType == 1)
  141. {
  142. tmp = h264bsdGetBits(pStrmData, 1);
  143. if (tmp == END_OF_STREAM)
  144. return(HANTRO_NOK);
  145. pSeqParamSet->deltaPicOrderAlwaysZeroFlag = (tmp == 1) ?
  146. HANTRO_TRUE : HANTRO_FALSE;
  147. tmp = h264bsdDecodeExpGolombSigned(pStrmData,
  148. &pSeqParamSet->offsetForNonRefPic);
  149. if (tmp != HANTRO_OK)
  150. return(tmp);
  151. tmp = h264bsdDecodeExpGolombSigned(pStrmData,
  152. &pSeqParamSet->offsetForTopToBottomField);
  153. if (tmp != HANTRO_OK)
  154. return(tmp);
  155. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  156. &pSeqParamSet->numRefFramesInPicOrderCntCycle);
  157. if (tmp != HANTRO_OK)
  158. return(tmp);
  159. if (pSeqParamSet->numRefFramesInPicOrderCntCycle > 255)
  160. {
  161. EPRINT("num_ref_frames_in_pic_order_cnt_cycle");
  162. return(HANTRO_NOK);
  163. }
  164. if (pSeqParamSet->numRefFramesInPicOrderCntCycle)
  165. {
  166. /* NOTE: This has to be freed somewhere! */
  167. ALLOCATE(pSeqParamSet->offsetForRefFrame,
  168. pSeqParamSet->numRefFramesInPicOrderCntCycle, i32);
  169. if (pSeqParamSet->offsetForRefFrame == NULL)
  170. return(MEMORY_ALLOCATION_ERROR);
  171. for (i = 0; i < pSeqParamSet->numRefFramesInPicOrderCntCycle; i++)
  172. {
  173. tmp = h264bsdDecodeExpGolombSigned(pStrmData,
  174. pSeqParamSet->offsetForRefFrame + i);
  175. if (tmp != HANTRO_OK)
  176. return(tmp);
  177. }
  178. }
  179. else
  180. {
  181. pSeqParamSet->offsetForRefFrame = NULL;
  182. }
  183. }
  184. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  185. &pSeqParamSet->numRefFrames);
  186. if (tmp != HANTRO_OK)
  187. return(tmp);
  188. if (pSeqParamSet->numRefFrames > MAX_NUM_REF_PICS)
  189. {
  190. EPRINT("num_ref_frames");
  191. return(HANTRO_NOK);
  192. }
  193. tmp = h264bsdGetBits(pStrmData, 1);
  194. if (tmp == END_OF_STREAM)
  195. return(HANTRO_NOK);
  196. pSeqParamSet->gapsInFrameNumValueAllowedFlag = (tmp == 1) ?
  197. HANTRO_TRUE : HANTRO_FALSE;
  198. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  199. if (tmp != HANTRO_OK)
  200. return(tmp);
  201. pSeqParamSet->picWidthInMbs = value + 1;
  202. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &value);
  203. if (tmp != HANTRO_OK)
  204. return(tmp);
  205. pSeqParamSet->picHeightInMbs = value + 1;
  206. /* frame_mbs_only_flag, shall be 1 for baseline profile */
  207. tmp = h264bsdGetBits(pStrmData, 1);
  208. if (tmp == END_OF_STREAM)
  209. return(HANTRO_NOK);
  210. if (!tmp)
  211. {
  212. EPRINT("frame_mbs_only_flag");
  213. return(HANTRO_NOK);
  214. }
  215. /* direct_8x8_inference_flag */
  216. tmp = h264bsdGetBits(pStrmData, 1);
  217. if (tmp == END_OF_STREAM)
  218. return(HANTRO_NOK);
  219. tmp = h264bsdGetBits(pStrmData, 1);
  220. if (tmp == END_OF_STREAM)
  221. return(HANTRO_NOK);
  222. pSeqParamSet->frameCroppingFlag = (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  223. if (pSeqParamSet->frameCroppingFlag)
  224. {
  225. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  226. &pSeqParamSet->frameCropLeftOffset);
  227. if (tmp != HANTRO_OK)
  228. return(tmp);
  229. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  230. &pSeqParamSet->frameCropRightOffset);
  231. if (tmp != HANTRO_OK)
  232. return(tmp);
  233. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  234. &pSeqParamSet->frameCropTopOffset);
  235. if (tmp != HANTRO_OK)
  236. return(tmp);
  237. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  238. &pSeqParamSet->frameCropBottomOffset);
  239. if (tmp != HANTRO_OK)
  240. return(tmp);
  241. /* check that frame cropping params are valid, parameters shall
  242. * specify non-negative area within the original picture */
  243. if ( ( (i32)pSeqParamSet->frameCropLeftOffset >
  244. ( 8 * (i32)pSeqParamSet->picWidthInMbs -
  245. ((i32)pSeqParamSet->frameCropRightOffset + 1) ) ) ||
  246. ( (i32)pSeqParamSet->frameCropTopOffset >
  247. ( 8 * (i32)pSeqParamSet->picHeightInMbs -
  248. ((i32)pSeqParamSet->frameCropBottomOffset + 1) ) ) )
  249. {
  250. EPRINT("frame_cropping");
  251. return(HANTRO_NOK);
  252. }
  253. }
  254. /* check that image dimensions and levelIdc match */
  255. tmp = pSeqParamSet->picWidthInMbs * pSeqParamSet->picHeightInMbs;
  256. value = GetDpbSize(tmp, pSeqParamSet->levelIdc);
  257. if (value == INVALID_DPB_SIZE || pSeqParamSet->numRefFrames > value)
  258. {
  259. DEBUG(("WARNING! Invalid DPB size based on SPS Level!\n"));
  260. DEBUG(("WARNING! Using num_ref_frames =%d for DPB size!\n",
  261. pSeqParamSet->numRefFrames));
  262. value = pSeqParamSet->numRefFrames;
  263. }
  264. pSeqParamSet->maxDpbSize = value;
  265. tmp = h264bsdGetBits(pStrmData, 1);
  266. if (tmp == END_OF_STREAM)
  267. return(HANTRO_NOK);
  268. pSeqParamSet->vuiParametersPresentFlag = (tmp == 1) ?
  269. HANTRO_TRUE : HANTRO_FALSE;
  270. /* VUI */
  271. if (pSeqParamSet->vuiParametersPresentFlag)
  272. {
  273. ALLOCATE(pSeqParamSet->vuiParameters, 1, vuiParameters_t);
  274. if (pSeqParamSet->vuiParameters == NULL)
  275. return(MEMORY_ALLOCATION_ERROR);
  276. tmp = h264bsdDecodeVuiParameters(pStrmData,
  277. pSeqParamSet->vuiParameters);
  278. if (tmp != HANTRO_OK)
  279. return(tmp);
  280. /* check numReorderFrames and maxDecFrameBuffering */
  281. if (pSeqParamSet->vuiParameters->bitstreamRestrictionFlag)
  282. {
  283. if (pSeqParamSet->vuiParameters->numReorderFrames >
  284. pSeqParamSet->vuiParameters->maxDecFrameBuffering ||
  285. pSeqParamSet->vuiParameters->maxDecFrameBuffering <
  286. pSeqParamSet->numRefFrames ||
  287. pSeqParamSet->vuiParameters->maxDecFrameBuffering >
  288. pSeqParamSet->maxDpbSize)
  289. {
  290. return(HANTRO_NOK);
  291. }
  292. /* standard says that "the sequence shall not require a DPB with
  293. * size of more than max(1, maxDecFrameBuffering) */
  294. pSeqParamSet->maxDpbSize =
  295. MAX(1, pSeqParamSet->vuiParameters->maxDecFrameBuffering);
  296. }
  297. }
  298. tmp = h264bsdRbspTrailingBits(pStrmData);
  299. /* ignore possible errors in trailing bits of parameters sets */
  300. return(HANTRO_OK);
  301. }
  302. /*------------------------------------------------------------------------------
  303. Function: GetDpbSize
  304. Functional description:
  305. Get size of the DPB in frames. Size is determined based on the
  306. picture size and MaxDPB for the specified level. These determine
  307. how many pictures may fit into to the buffer. However, the size
  308. is also limited to a maximum of 16 frames and therefore function
  309. returns the minimum of the determined size and 16.
  310. Inputs:
  311. picSizeInMbs number of macroblocks in the picture
  312. levelIdc indicates the level
  313. Outputs:
  314. none
  315. Returns:
  316. size of the DPB in frames
  317. INVALID_DPB_SIZE when invalid levelIdc specified or picSizeInMbs
  318. is higher than supported by the level in question
  319. ------------------------------------------------------------------------------*/
  320. u32 GetDpbSize(u32 picSizeInMbs, u32 levelIdc)
  321. {
  322. /* Variables */
  323. u32 tmp;
  324. u32 maxPicSizeInMbs;
  325. /* Code */
  326. ASSERT(picSizeInMbs);
  327. /* use tmp as the size of the DPB in bytes, computes as 1024 * MaxDPB
  328. * (from table A-1 in Annex A) */
  329. switch (levelIdc)
  330. {
  331. case 10:
  332. tmp = 152064;
  333. maxPicSizeInMbs = 99;
  334. break;
  335. case 11:
  336. tmp = 345600;
  337. maxPicSizeInMbs = 396;
  338. break;
  339. case 12:
  340. tmp = 912384;
  341. maxPicSizeInMbs = 396;
  342. break;
  343. case 13:
  344. tmp = 912384;
  345. maxPicSizeInMbs = 396;
  346. break;
  347. case 20:
  348. tmp = 912384;
  349. maxPicSizeInMbs = 396;
  350. break;
  351. case 21:
  352. tmp = 1824768;
  353. maxPicSizeInMbs = 792;
  354. break;
  355. case 22:
  356. tmp = 3110400;
  357. maxPicSizeInMbs = 1620;
  358. break;
  359. case 30:
  360. tmp = 3110400;
  361. maxPicSizeInMbs = 1620;
  362. break;
  363. case 31:
  364. tmp = 6912000;
  365. maxPicSizeInMbs = 3600;
  366. break;
  367. case 32:
  368. tmp = 7864320;
  369. maxPicSizeInMbs = 5120;
  370. break;
  371. case 40:
  372. tmp = 12582912;
  373. maxPicSizeInMbs = 8192;
  374. break;
  375. case 41:
  376. tmp = 12582912;
  377. maxPicSizeInMbs = 8192;
  378. break;
  379. case 42:
  380. tmp = 34816*384;
  381. maxPicSizeInMbs = 8704;
  382. break;
  383. case 50:
  384. /* standard says 42301440 here, but corrigendum "corrects" this to
  385. * 42393600 */
  386. tmp = 42393600;
  387. maxPicSizeInMbs = 22080;
  388. break;
  389. case 51:
  390. tmp = 70778880;
  391. maxPicSizeInMbs = 36864;
  392. break;
  393. default:
  394. return(INVALID_DPB_SIZE);
  395. }
  396. /* this is not "correct" return value! However, it results in error in
  397. * decoding and this was easiest place to check picture size */
  398. if (picSizeInMbs > maxPicSizeInMbs)
  399. return(INVALID_DPB_SIZE);
  400. tmp /= (picSizeInMbs*384);
  401. return(MIN(tmp, 16));
  402. }
  403. /*------------------------------------------------------------------------------
  404. Function name: h264bsdCompareSeqParamSets
  405. Functional description:
  406. Compare two sequence parameter sets.
  407. Inputs:
  408. pSps1 pointer to a sequence parameter set
  409. pSps2 pointer to another sequence parameter set
  410. Outputs:
  411. 0 sequence parameter sets are equal
  412. 1 otherwise
  413. ------------------------------------------------------------------------------*/
  414. u32 h264bsdCompareSeqParamSets(seqParamSet_t *pSps1, seqParamSet_t *pSps2)
  415. {
  416. /* Variables */
  417. u32 i;
  418. /* Code */
  419. ASSERT(pSps1);
  420. ASSERT(pSps2);
  421. /* first compare parameters whose existence does not depend on other
  422. * parameters and only compare the rest of the params if these are equal */
  423. if (pSps1->profileIdc == pSps2->profileIdc &&
  424. pSps1->levelIdc == pSps2->levelIdc &&
  425. pSps1->maxFrameNum == pSps2->maxFrameNum &&
  426. pSps1->picOrderCntType == pSps2->picOrderCntType &&
  427. pSps1->numRefFrames == pSps2->numRefFrames &&
  428. pSps1->gapsInFrameNumValueAllowedFlag ==
  429. pSps2->gapsInFrameNumValueAllowedFlag &&
  430. pSps1->picWidthInMbs == pSps2->picWidthInMbs &&
  431. pSps1->picHeightInMbs == pSps2->picHeightInMbs &&
  432. pSps1->frameCroppingFlag == pSps2->frameCroppingFlag &&
  433. pSps1->vuiParametersPresentFlag == pSps2->vuiParametersPresentFlag)
  434. {
  435. if (pSps1->picOrderCntType == 0)
  436. {
  437. if (pSps1->maxPicOrderCntLsb != pSps2->maxPicOrderCntLsb)
  438. return 1;
  439. }
  440. else if (pSps1->picOrderCntType == 1)
  441. {
  442. if (pSps1->deltaPicOrderAlwaysZeroFlag !=
  443. pSps2->deltaPicOrderAlwaysZeroFlag ||
  444. pSps1->offsetForNonRefPic != pSps2->offsetForNonRefPic ||
  445. pSps1->offsetForTopToBottomField !=
  446. pSps2->offsetForTopToBottomField ||
  447. pSps1->numRefFramesInPicOrderCntCycle !=
  448. pSps2->numRefFramesInPicOrderCntCycle)
  449. {
  450. return 1;
  451. }
  452. else
  453. {
  454. for (i = 0; i < pSps1->numRefFramesInPicOrderCntCycle; i++)
  455. if (pSps1->offsetForRefFrame[i] !=
  456. pSps2->offsetForRefFrame[i])
  457. {
  458. return 1;
  459. }
  460. }
  461. }
  462. if (pSps1->frameCroppingFlag)
  463. {
  464. if (pSps1->frameCropLeftOffset != pSps2->frameCropLeftOffset ||
  465. pSps1->frameCropRightOffset != pSps2->frameCropRightOffset ||
  466. pSps1->frameCropTopOffset != pSps2->frameCropTopOffset ||
  467. pSps1->frameCropBottomOffset != pSps2->frameCropBottomOffset)
  468. {
  469. return 1;
  470. }
  471. }
  472. return 0;
  473. }
  474. return 1;
  475. }