PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/H264Dec/source/h264bsd_vui.c

http://github.com/mbebenita/Broadway
C | 490 lines | 326 code | 70 blank | 94 comment | 130 complexity | 12d6bc60f57697644a76f180b88aa26e 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. h264bsdDecodeVuiParameters
  24. DecodeHrdParameters
  25. ------------------------------------------------------------------------------*/
  26. /*------------------------------------------------------------------------------
  27. 1. Include headers
  28. ------------------------------------------------------------------------------*/
  29. #include "h264bsd_vui.h"
  30. #include "basetype.h"
  31. #include "h264bsd_vlc.h"
  32. #include "h264bsd_stream.h"
  33. #include "h264bsd_util.h"
  34. /*------------------------------------------------------------------------------
  35. 2. External compiler flags
  36. --------------------------------------------------------------------------------
  37. --------------------------------------------------------------------------------
  38. 3. Module defines
  39. ------------------------------------------------------------------------------*/
  40. #define MAX_DPB_SIZE 16
  41. #define MAX_BR 240000 /* for level 5.1 */
  42. #define MAX_CPB 240000 /* for level 5.1 */
  43. /*------------------------------------------------------------------------------
  44. 4. Local function prototypes
  45. ------------------------------------------------------------------------------*/
  46. static u32 DecodeHrdParameters(
  47. strmData_t *pStrmData,
  48. hrdParameters_t *pHrdParameters);
  49. /*------------------------------------------------------------------------------
  50. Function: h264bsdDecodeVuiParameters
  51. Functional description:
  52. Decode VUI parameters from the stream. See standard for details.
  53. Inputs:
  54. pStrmData pointer to stream data structure
  55. Outputs:
  56. pVuiParameters decoded information is stored here
  57. Returns:
  58. HANTRO_OK success
  59. HANTRO_NOK invalid stream data or end of stream
  60. ------------------------------------------------------------------------------*/
  61. u32 h264bsdDecodeVuiParameters(strmData_t *pStrmData,
  62. vuiParameters_t *pVuiParameters)
  63. {
  64. /* Variables */
  65. u32 tmp;
  66. /* Code */
  67. ASSERT(pStrmData);
  68. ASSERT(pVuiParameters);
  69. H264SwDecMemset(pVuiParameters, 0, sizeof(vuiParameters_t));
  70. tmp = h264bsdGetBits(pStrmData, 1);
  71. if (tmp == END_OF_STREAM)
  72. return(HANTRO_NOK);
  73. pVuiParameters->aspectRatioPresentFlag = (tmp == 1) ?
  74. HANTRO_TRUE : HANTRO_FALSE;
  75. if (pVuiParameters->aspectRatioPresentFlag)
  76. {
  77. tmp = h264bsdGetBits(pStrmData, 8);
  78. if (tmp == END_OF_STREAM)
  79. return(HANTRO_NOK);
  80. pVuiParameters->aspectRatioIdc = tmp;
  81. if (pVuiParameters->aspectRatioIdc == ASPECT_RATIO_EXTENDED_SAR)
  82. {
  83. tmp = h264bsdGetBits(pStrmData, 16);
  84. if (tmp == END_OF_STREAM)
  85. return(HANTRO_NOK);
  86. pVuiParameters->sarWidth = tmp;
  87. tmp = h264bsdGetBits(pStrmData, 16);
  88. if (tmp == END_OF_STREAM)
  89. return(HANTRO_NOK);
  90. pVuiParameters->sarHeight = tmp;
  91. }
  92. }
  93. tmp = h264bsdGetBits(pStrmData, 1);
  94. if (tmp == END_OF_STREAM)
  95. return(HANTRO_NOK);
  96. pVuiParameters->overscanInfoPresentFlag = (tmp == 1) ?
  97. HANTRO_TRUE : HANTRO_FALSE;
  98. if (pVuiParameters->overscanInfoPresentFlag)
  99. {
  100. tmp = h264bsdGetBits(pStrmData, 1);
  101. if (tmp == END_OF_STREAM)
  102. return(HANTRO_NOK);
  103. pVuiParameters->overscanAppropriateFlag = (tmp == 1) ?
  104. HANTRO_TRUE : HANTRO_FALSE;
  105. }
  106. tmp = h264bsdGetBits(pStrmData, 1);
  107. if (tmp == END_OF_STREAM)
  108. return(HANTRO_NOK);
  109. pVuiParameters->videoSignalTypePresentFlag = (tmp == 1) ?
  110. HANTRO_TRUE : HANTRO_FALSE;
  111. if (pVuiParameters->videoSignalTypePresentFlag)
  112. {
  113. tmp = h264bsdGetBits(pStrmData, 3);
  114. if (tmp == END_OF_STREAM)
  115. return(HANTRO_NOK);
  116. pVuiParameters->videoFormat = tmp;
  117. tmp = h264bsdGetBits(pStrmData, 1);
  118. if (tmp == END_OF_STREAM)
  119. return(HANTRO_NOK);
  120. pVuiParameters->videoFullRangeFlag = (tmp == 1) ?
  121. HANTRO_TRUE : HANTRO_FALSE;
  122. tmp = h264bsdGetBits(pStrmData, 1);
  123. if (tmp == END_OF_STREAM)
  124. return(HANTRO_NOK);
  125. pVuiParameters->colourDescriptionPresentFlag =
  126. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  127. if (pVuiParameters->colourDescriptionPresentFlag)
  128. {
  129. tmp = h264bsdGetBits(pStrmData, 8);
  130. if (tmp == END_OF_STREAM)
  131. return(HANTRO_NOK);
  132. pVuiParameters->colourPrimaries = tmp;
  133. tmp = h264bsdGetBits(pStrmData, 8);
  134. if (tmp == END_OF_STREAM)
  135. return(HANTRO_NOK);
  136. pVuiParameters->transferCharacteristics = tmp;
  137. tmp = h264bsdGetBits(pStrmData, 8);
  138. if (tmp == END_OF_STREAM)
  139. return(HANTRO_NOK);
  140. pVuiParameters->matrixCoefficients = tmp;
  141. }
  142. else
  143. {
  144. pVuiParameters->colourPrimaries = 2;
  145. pVuiParameters->transferCharacteristics = 2;
  146. pVuiParameters->matrixCoefficients = 2;
  147. }
  148. }
  149. else
  150. {
  151. pVuiParameters->videoFormat = 5;
  152. pVuiParameters->colourPrimaries = 2;
  153. pVuiParameters->transferCharacteristics = 2;
  154. pVuiParameters->matrixCoefficients = 2;
  155. }
  156. tmp = h264bsdGetBits(pStrmData, 1);
  157. if (tmp == END_OF_STREAM)
  158. return(HANTRO_NOK);
  159. pVuiParameters->chromaLocInfoPresentFlag =
  160. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  161. if (pVuiParameters->chromaLocInfoPresentFlag)
  162. {
  163. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  164. &pVuiParameters->chromaSampleLocTypeTopField);
  165. if (tmp != HANTRO_OK)
  166. return(tmp);
  167. if (pVuiParameters->chromaSampleLocTypeTopField > 5)
  168. return(HANTRO_NOK);
  169. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  170. &pVuiParameters->chromaSampleLocTypeBottomField);
  171. if (tmp != HANTRO_OK)
  172. return(tmp);
  173. if (pVuiParameters->chromaSampleLocTypeBottomField > 5)
  174. return(HANTRO_NOK);
  175. }
  176. tmp = h264bsdGetBits(pStrmData, 1);
  177. if (tmp == END_OF_STREAM)
  178. return(HANTRO_NOK);
  179. pVuiParameters->timingInfoPresentFlag =
  180. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  181. if (pVuiParameters->timingInfoPresentFlag)
  182. {
  183. tmp = h264bsdShowBits32(pStrmData);
  184. if (h264bsdFlushBits(pStrmData, 32) == END_OF_STREAM)
  185. return(HANTRO_NOK);
  186. if (tmp == 0)
  187. return(HANTRO_NOK);
  188. pVuiParameters->numUnitsInTick = tmp;
  189. tmp = h264bsdShowBits32(pStrmData);
  190. if (h264bsdFlushBits(pStrmData, 32) == END_OF_STREAM)
  191. return(HANTRO_NOK);
  192. if (tmp == 0)
  193. return(HANTRO_NOK);
  194. pVuiParameters->timeScale = tmp;
  195. tmp = h264bsdGetBits(pStrmData, 1);
  196. if (tmp == END_OF_STREAM)
  197. return(HANTRO_NOK);
  198. pVuiParameters->fixedFrameRateFlag =
  199. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  200. }
  201. tmp = h264bsdGetBits(pStrmData, 1);
  202. if (tmp == END_OF_STREAM)
  203. return(HANTRO_NOK);
  204. pVuiParameters->nalHrdParametersPresentFlag =
  205. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  206. if (pVuiParameters->nalHrdParametersPresentFlag)
  207. {
  208. tmp = DecodeHrdParameters(pStrmData, &pVuiParameters->nalHrdParameters);
  209. if (tmp != HANTRO_OK)
  210. return(tmp);
  211. }
  212. else
  213. {
  214. pVuiParameters->nalHrdParameters.cpbCnt = 1;
  215. /* MaxBR and MaxCPB should be the values correspondig to the levelIdc
  216. * in the SPS containing these VUI parameters. However, these values
  217. * are not used anywhere and maximum for any level will be used here */
  218. pVuiParameters->nalHrdParameters.bitRateValue[0] = 1200 * MAX_BR + 1;
  219. pVuiParameters->nalHrdParameters.cpbSizeValue[0] = 1200 * MAX_CPB + 1;
  220. pVuiParameters->nalHrdParameters.initialCpbRemovalDelayLength = 24;
  221. pVuiParameters->nalHrdParameters.cpbRemovalDelayLength = 24;
  222. pVuiParameters->nalHrdParameters.dpbOutputDelayLength = 24;
  223. pVuiParameters->nalHrdParameters.timeOffsetLength = 24;
  224. }
  225. tmp = h264bsdGetBits(pStrmData, 1);
  226. if (tmp == END_OF_STREAM)
  227. return(HANTRO_NOK);
  228. pVuiParameters->vclHrdParametersPresentFlag =
  229. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  230. if (pVuiParameters->vclHrdParametersPresentFlag)
  231. {
  232. tmp = DecodeHrdParameters(pStrmData, &pVuiParameters->vclHrdParameters);
  233. if (tmp != HANTRO_OK)
  234. return(tmp);
  235. }
  236. else
  237. {
  238. pVuiParameters->vclHrdParameters.cpbCnt = 1;
  239. /* MaxBR and MaxCPB should be the values correspondig to the levelIdc
  240. * in the SPS containing these VUI parameters. However, these values
  241. * are not used anywhere and maximum for any level will be used here */
  242. pVuiParameters->vclHrdParameters.bitRateValue[0] = 1000 * MAX_BR + 1;
  243. pVuiParameters->vclHrdParameters.cpbSizeValue[0] = 1000 * MAX_CPB + 1;
  244. pVuiParameters->vclHrdParameters.initialCpbRemovalDelayLength = 24;
  245. pVuiParameters->vclHrdParameters.cpbRemovalDelayLength = 24;
  246. pVuiParameters->vclHrdParameters.dpbOutputDelayLength = 24;
  247. pVuiParameters->vclHrdParameters.timeOffsetLength = 24;
  248. }
  249. if (pVuiParameters->nalHrdParametersPresentFlag ||
  250. pVuiParameters->vclHrdParametersPresentFlag)
  251. {
  252. tmp = h264bsdGetBits(pStrmData, 1);
  253. if (tmp == END_OF_STREAM)
  254. return(HANTRO_NOK);
  255. pVuiParameters->lowDelayHrdFlag =
  256. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  257. }
  258. tmp = h264bsdGetBits(pStrmData, 1);
  259. if (tmp == END_OF_STREAM)
  260. return(HANTRO_NOK);
  261. pVuiParameters->picStructPresentFlag =
  262. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  263. tmp = h264bsdGetBits(pStrmData, 1);
  264. if (tmp == END_OF_STREAM)
  265. return(HANTRO_NOK);
  266. pVuiParameters->bitstreamRestrictionFlag =
  267. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  268. if (pVuiParameters->bitstreamRestrictionFlag)
  269. {
  270. tmp = h264bsdGetBits(pStrmData, 1);
  271. if (tmp == END_OF_STREAM)
  272. return(HANTRO_NOK);
  273. pVuiParameters->motionVectorsOverPicBoundariesFlag =
  274. (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  275. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  276. &pVuiParameters->maxBytesPerPicDenom);
  277. if (tmp != HANTRO_OK)
  278. return(tmp);
  279. if (pVuiParameters->maxBytesPerPicDenom > 16)
  280. return(HANTRO_NOK);
  281. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  282. &pVuiParameters->maxBitsPerMbDenom);
  283. if (tmp != HANTRO_OK)
  284. return(tmp);
  285. if (pVuiParameters->maxBitsPerMbDenom > 16)
  286. return(HANTRO_NOK);
  287. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  288. &pVuiParameters->log2MaxMvLengthHorizontal);
  289. if (tmp != HANTRO_OK)
  290. return(tmp);
  291. if (pVuiParameters->log2MaxMvLengthHorizontal > 16)
  292. return(HANTRO_NOK);
  293. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  294. &pVuiParameters->log2MaxMvLengthVertical);
  295. if (tmp != HANTRO_OK)
  296. return(tmp);
  297. if (pVuiParameters->log2MaxMvLengthVertical > 16)
  298. return(HANTRO_NOK);
  299. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  300. &pVuiParameters->numReorderFrames);
  301. if (tmp != HANTRO_OK)
  302. return(tmp);
  303. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  304. &pVuiParameters->maxDecFrameBuffering);
  305. if (tmp != HANTRO_OK)
  306. return(tmp);
  307. }
  308. else
  309. {
  310. pVuiParameters->motionVectorsOverPicBoundariesFlag = HANTRO_TRUE;
  311. pVuiParameters->maxBytesPerPicDenom = 2;
  312. pVuiParameters->maxBitsPerMbDenom = 1;
  313. pVuiParameters->log2MaxMvLengthHorizontal = 16;
  314. pVuiParameters->log2MaxMvLengthVertical = 16;
  315. pVuiParameters->numReorderFrames = MAX_DPB_SIZE;
  316. pVuiParameters->maxDecFrameBuffering = MAX_DPB_SIZE;
  317. }
  318. return(HANTRO_OK);
  319. }
  320. /*------------------------------------------------------------------------------
  321. Function: DecodeHrdParameters
  322. Functional description:
  323. Decode HRD parameters from the stream. See standard for details.
  324. Inputs:
  325. pStrmData pointer to stream data structure
  326. Outputs:
  327. pHrdParameters decoded information is stored here
  328. Returns:
  329. HANTRO_OK success
  330. HANTRO_NOK invalid stream data
  331. ------------------------------------------------------------------------------*/
  332. static u32 DecodeHrdParameters(
  333. strmData_t *pStrmData,
  334. hrdParameters_t *pHrdParameters)
  335. {
  336. /* Variables */
  337. u32 tmp, i;
  338. /* Code */
  339. ASSERT(pStrmData);
  340. ASSERT(pHrdParameters);
  341. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &pHrdParameters->cpbCnt);
  342. if (tmp != HANTRO_OK)
  343. return(tmp);
  344. /* cpbCount = cpb_cnt_minus1 + 1 */
  345. pHrdParameters->cpbCnt++;
  346. if (pHrdParameters->cpbCnt > MAX_CPB_CNT)
  347. return(HANTRO_NOK);
  348. tmp = h264bsdGetBits(pStrmData, 4);
  349. if (tmp == END_OF_STREAM)
  350. return(HANTRO_NOK);
  351. pHrdParameters->bitRateScale = tmp;
  352. tmp = h264bsdGetBits(pStrmData, 4);
  353. if (tmp == END_OF_STREAM)
  354. return(HANTRO_NOK);
  355. pHrdParameters->cpbSizeScale = tmp;
  356. for (i = 0; i < pHrdParameters->cpbCnt; i++)
  357. {
  358. /* bit_rate_value_minus1 in the range [0, 2^32 - 2] */
  359. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  360. &pHrdParameters->bitRateValue[i]);
  361. if (tmp != HANTRO_OK)
  362. return(tmp);
  363. if (pHrdParameters->bitRateValue[i] > 4294967294U)
  364. return(HANTRO_NOK);
  365. pHrdParameters->bitRateValue[i]++;
  366. /* this may result in overflow, but this value is not used for
  367. * anything */
  368. pHrdParameters->bitRateValue[i] *=
  369. 1 << (6 + pHrdParameters->bitRateScale);
  370. /* cpb_size_value_minus1 in the range [0, 2^32 - 2] */
  371. tmp = h264bsdDecodeExpGolombUnsigned(pStrmData,
  372. &pHrdParameters->cpbSizeValue[i]);
  373. if (tmp != HANTRO_OK)
  374. return(tmp);
  375. if (pHrdParameters->cpbSizeValue[i] > 4294967294U)
  376. return(HANTRO_NOK);
  377. pHrdParameters->cpbSizeValue[i]++;
  378. /* this may result in overflow, but this value is not used for
  379. * anything */
  380. pHrdParameters->cpbSizeValue[i] *=
  381. 1 << (4 + pHrdParameters->cpbSizeScale);
  382. tmp = h264bsdGetBits(pStrmData, 1);
  383. if (tmp == END_OF_STREAM)
  384. return(HANTRO_NOK);
  385. pHrdParameters->cbrFlag[i] = (tmp == 1) ? HANTRO_TRUE : HANTRO_FALSE;
  386. }
  387. tmp = h264bsdGetBits(pStrmData, 5);
  388. if (tmp == END_OF_STREAM)
  389. return(HANTRO_NOK);
  390. pHrdParameters->initialCpbRemovalDelayLength = tmp + 1;
  391. tmp = h264bsdGetBits(pStrmData, 5);
  392. if (tmp == END_OF_STREAM)
  393. return(HANTRO_NOK);
  394. pHrdParameters->cpbRemovalDelayLength = tmp + 1;
  395. tmp = h264bsdGetBits(pStrmData, 5);
  396. if (tmp == END_OF_STREAM)
  397. return(HANTRO_NOK);
  398. pHrdParameters->dpbOutputDelayLength = tmp + 1;
  399. tmp = h264bsdGetBits(pStrmData, 5);
  400. if (tmp == END_OF_STREAM)
  401. return(HANTRO_NOK);
  402. pHrdParameters->timeOffsetLength = tmp;
  403. return(HANTRO_OK);
  404. }