PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/H264Dec/source/h264bsd_decoder.c

http://github.com/mbebenita/Broadway
C | 961 lines | 490 code | 139 blank | 332 comment | 87 complexity | de932a57348bac29513775ee00eb738e 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. h264bsdInit
  24. h264bsdDecode
  25. h264bsdShutdown
  26. h264bsdCurrentImage
  27. h264bsdNextOutputPicture
  28. h264bsdPicWidth
  29. h264bsdPicHeight
  30. h264bsdFlushBuffer
  31. h264bsdCheckValidParamSets
  32. h264bsdVideoRange
  33. h264bsdMatrixCoefficients
  34. h264bsdCroppingParams
  35. ------------------------------------------------------------------------------*/
  36. /*------------------------------------------------------------------------------
  37. 1. Include headers
  38. ------------------------------------------------------------------------------*/
  39. #include "h264bsd_decoder.h"
  40. #include "h264bsd_nal_unit.h"
  41. #include "h264bsd_byte_stream.h"
  42. #include "h264bsd_seq_param_set.h"
  43. #include "h264bsd_pic_param_set.h"
  44. #include "h264bsd_slice_header.h"
  45. #include "h264bsd_slice_data.h"
  46. #include "h264bsd_neighbour.h"
  47. #include "h264bsd_util.h"
  48. #include "h264bsd_dpb.h"
  49. #include "h264bsd_deblocking.h"
  50. #include "h264bsd_conceal.h"
  51. /*------------------------------------------------------------------------------
  52. 2. External compiler flags
  53. --------------------------------------------------------------------------------
  54. --------------------------------------------------------------------------------
  55. 3. Module defines
  56. ------------------------------------------------------------------------------*/
  57. /*------------------------------------------------------------------------------
  58. 4. Local function prototypes
  59. ------------------------------------------------------------------------------*/
  60. /*------------------------------------------------------------------------------
  61. Function name: h264bsdInit
  62. Functional description:
  63. Initialize the decoder.
  64. Inputs:
  65. noOutputReordering flag to indicate the decoder that it does not
  66. have to perform reordering of display images.
  67. Outputs:
  68. pStorage pointer to initialized storage structure
  69. Returns:
  70. none
  71. ------------------------------------------------------------------------------*/
  72. u32 h264bsdInit(storage_t *pStorage, u32 noOutputReordering)
  73. {
  74. /* Variables */
  75. u32 size;
  76. /* Code */
  77. ASSERT(pStorage);
  78. h264bsdInitStorage(pStorage);
  79. /* allocate mbLayer to be next multiple of 64 to enable use of
  80. * specific NEON optimized "memset" for clearing the structure */
  81. size = (sizeof(macroblockLayer_t) + 63) & ~0x3F;
  82. pStorage->mbLayer = (macroblockLayer_t*)H264SwDecMalloc(size);
  83. if (!pStorage->mbLayer)
  84. return HANTRO_NOK;
  85. if (noOutputReordering)
  86. pStorage->noReordering = HANTRO_TRUE;
  87. return HANTRO_OK;
  88. }
  89. /*------------------------------------------------------------------------------
  90. Function: h264bsdDecode
  91. Functional description:
  92. Decode a NAL unit. This function calls other modules to perform
  93. tasks like
  94. * extract and decode NAL unit from the byte stream
  95. * decode parameter sets
  96. * decode slice header and slice data
  97. * conceal errors in the picture
  98. * perform deblocking filtering
  99. This function contains top level control logic of the decoder.
  100. Inputs:
  101. pStorage pointer to storage data structure
  102. byteStrm pointer to stream buffer given by application
  103. len length of the buffer in bytes
  104. picId identifier for a picture, assigned by the
  105. application
  106. Outputs:
  107. readBytes number of bytes read from the stream is stored
  108. here
  109. Returns:
  110. H264BSD_RDY decoding finished, nothing special
  111. H264BSD_PIC_RDY decoding of a picture finished
  112. H264BSD_HDRS_RDY param sets activated, information like
  113. picture dimensions etc can be read
  114. H264BSD_ERROR error in decoding
  115. H264BSD_PARAM_SET_ERROR serius error in decoding, failed to
  116. activate param sets
  117. ------------------------------------------------------------------------------*/
  118. u32 h264bsdDecode(storage_t *pStorage, u8 *byteStrm, u32 len, u32 picId,
  119. u32 *readBytes)
  120. {
  121. /* Variables */
  122. u32 tmp, ppsId, spsId;
  123. i32 picOrderCnt;
  124. nalUnit_t nalUnit;
  125. seqParamSet_t seqParamSet;
  126. picParamSet_t picParamSet;
  127. strmData_t strm;
  128. u32 accessUnitBoundaryFlag = HANTRO_FALSE;
  129. u32 picReady = HANTRO_FALSE;
  130. /* Code */
  131. ASSERT(pStorage);
  132. ASSERT(byteStrm);
  133. ASSERT(len);
  134. ASSERT(readBytes);
  135. /* if previous buffer was not finished and same pointer given -> skip NAL
  136. * unit extraction */
  137. if (pStorage->prevBufNotFinished && byteStrm == pStorage->prevBufPointer)
  138. {
  139. strm = pStorage->strm[0];
  140. strm.pStrmCurrPos = strm.pStrmBuffStart;
  141. strm.strmBuffReadBits = strm.bitPosInWord = 0;
  142. *readBytes = pStorage->prevBytesConsumed;
  143. }
  144. else
  145. {
  146. tmp = h264bsdExtractNalUnit(byteStrm, len, &strm, readBytes);
  147. if (tmp != HANTRO_OK)
  148. {
  149. EPRINT("BYTE_STREAM");
  150. return(H264BSD_ERROR);
  151. }
  152. /* store stream */
  153. pStorage->strm[0] = strm;
  154. pStorage->prevBytesConsumed = *readBytes;
  155. pStorage->prevBufPointer = byteStrm;
  156. }
  157. pStorage->prevBufNotFinished = HANTRO_FALSE;
  158. tmp = h264bsdDecodeNalUnit(&strm, &nalUnit);
  159. if (tmp != HANTRO_OK)
  160. {
  161. EPRINT("NAL_UNIT");
  162. return(H264BSD_ERROR);
  163. }
  164. /* Discard unspecified, reserved, SPS extension and auxiliary picture slices */
  165. if(nalUnit.nalUnitType == 0 || nalUnit.nalUnitType >= 13)
  166. {
  167. DEBUG(("DISCARDED NAL (UNSPECIFIED, REGISTERED, SPS ext or AUX slice)\n"));
  168. return(H264BSD_RDY);
  169. }
  170. tmp = h264bsdCheckAccessUnitBoundary(
  171. &strm,
  172. &nalUnit,
  173. pStorage,
  174. &accessUnitBoundaryFlag);
  175. if (tmp != HANTRO_OK)
  176. {
  177. EPRINT("ACCESS UNIT BOUNDARY CHECK");
  178. if (tmp == PARAM_SET_ERROR)
  179. return(H264BSD_PARAM_SET_ERROR);
  180. else
  181. return(H264BSD_ERROR);
  182. }
  183. if ( accessUnitBoundaryFlag )
  184. {
  185. DEBUG(("Access unit boundary\n"));
  186. /* conceal if picture started and param sets activated */
  187. if (pStorage->picStarted && pStorage->activeSps != NULL)
  188. {
  189. DEBUG(("CONCEALING..."));
  190. /* return error if second phase of
  191. * initialization is not completed */
  192. if (pStorage->pendingActivation)
  193. {
  194. EPRINT("Pending activation not completed");
  195. return (H264BSD_ERROR);
  196. }
  197. if (!pStorage->validSliceInAccessUnit)
  198. {
  199. pStorage->currImage->data =
  200. h264bsdAllocateDpbImage(pStorage->dpb);
  201. h264bsdInitRefPicList(pStorage->dpb);
  202. tmp = h264bsdConceal(pStorage, pStorage->currImage, P_SLICE);
  203. }
  204. else
  205. tmp = h264bsdConceal(pStorage, pStorage->currImage,
  206. pStorage->sliceHeader->sliceType);
  207. picReady = HANTRO_TRUE;
  208. /* current NAL unit should be decoded on next activation -> set
  209. * readBytes to 0 */
  210. *readBytes = 0;
  211. pStorage->prevBufNotFinished = HANTRO_TRUE;
  212. DEBUG(("...DONE\n"));
  213. }
  214. else
  215. {
  216. pStorage->validSliceInAccessUnit = HANTRO_FALSE;
  217. }
  218. pStorage->skipRedundantSlices = HANTRO_FALSE;
  219. }
  220. if (!picReady)
  221. {
  222. switch (nalUnit.nalUnitType)
  223. {
  224. case NAL_SEQ_PARAM_SET:
  225. DEBUG(("SEQ PARAM SET\n"));
  226. tmp = h264bsdDecodeSeqParamSet(&strm, &seqParamSet);
  227. if (tmp != HANTRO_OK)
  228. {
  229. EPRINT("SEQ_PARAM_SET");
  230. FREE(seqParamSet.offsetForRefFrame);
  231. FREE(seqParamSet.vuiParameters);
  232. return(H264BSD_ERROR);
  233. }
  234. tmp = h264bsdStoreSeqParamSet(pStorage, &seqParamSet);
  235. break;
  236. case NAL_PIC_PARAM_SET:
  237. DEBUG(("PIC PARAM SET\n"));
  238. tmp = h264bsdDecodePicParamSet(&strm, &picParamSet);
  239. if (tmp != HANTRO_OK)
  240. {
  241. EPRINT("PIC_PARAM_SET");
  242. FREE(picParamSet.runLength);
  243. FREE(picParamSet.topLeft);
  244. FREE(picParamSet.bottomRight);
  245. FREE(picParamSet.sliceGroupId);
  246. return(H264BSD_ERROR);
  247. }
  248. tmp = h264bsdStorePicParamSet(pStorage, &picParamSet);
  249. break;
  250. case NAL_CODED_SLICE_IDR:
  251. DEBUG(("IDR "));
  252. /* fall through */
  253. case NAL_CODED_SLICE:
  254. DEBUG(("SLICE HEADER\n"));
  255. /* picture successfully finished and still decoding same old
  256. * access unit -> no need to decode redundant slices */
  257. if (pStorage->skipRedundantSlices)
  258. return(H264BSD_RDY);
  259. pStorage->picStarted = HANTRO_TRUE;
  260. if (h264bsdIsStartOfPicture(pStorage))
  261. {
  262. pStorage->numConcealedMbs = 0;
  263. pStorage->currentPicId = picId;
  264. tmp = h264bsdCheckPpsId(&strm, &ppsId);
  265. ASSERT(tmp == HANTRO_OK);
  266. /* store old activeSpsId and return headers ready
  267. * indication if activeSps changes */
  268. spsId = pStorage->activeSpsId;
  269. tmp = h264bsdActivateParamSets(pStorage, ppsId,
  270. IS_IDR_NAL_UNIT(&nalUnit) ?
  271. HANTRO_TRUE : HANTRO_FALSE);
  272. if (tmp != HANTRO_OK)
  273. {
  274. EPRINT("Param set activation");
  275. pStorage->activePpsId = MAX_NUM_PIC_PARAM_SETS;
  276. pStorage->activePps = NULL;
  277. pStorage->activeSpsId = MAX_NUM_SEQ_PARAM_SETS;
  278. pStorage->activeSps = NULL;
  279. pStorage->pendingActivation = HANTRO_FALSE;
  280. if(tmp == MEMORY_ALLOCATION_ERROR)
  281. {
  282. return H264BSD_MEMALLOC_ERROR;
  283. }
  284. else
  285. return(H264BSD_PARAM_SET_ERROR);
  286. }
  287. if (spsId != pStorage->activeSpsId)
  288. {
  289. seqParamSet_t *oldSPS = NULL;
  290. seqParamSet_t *newSPS = pStorage->activeSps;
  291. u32 noOutputOfPriorPicsFlag = 1;
  292. if(pStorage->oldSpsId < MAX_NUM_SEQ_PARAM_SETS)
  293. {
  294. oldSPS = pStorage->sps[pStorage->oldSpsId];
  295. }
  296. *readBytes = 0;
  297. pStorage->prevBufNotFinished = HANTRO_TRUE;
  298. if(nalUnit.nalUnitType == NAL_CODED_SLICE_IDR)
  299. {
  300. tmp =
  301. h264bsdCheckPriorPicsFlag(&noOutputOfPriorPicsFlag,
  302. &strm, newSPS,
  303. pStorage->activePps,
  304. nalUnit.nalUnitType);
  305. }
  306. else
  307. {
  308. tmp = HANTRO_NOK;
  309. }
  310. if((tmp != HANTRO_OK) ||
  311. (noOutputOfPriorPicsFlag != 0) ||
  312. (pStorage->dpb->noReordering) ||
  313. (oldSPS == NULL) ||
  314. (oldSPS->picWidthInMbs != newSPS->picWidthInMbs) ||
  315. (oldSPS->picHeightInMbs != newSPS->picHeightInMbs) ||
  316. (oldSPS->maxDpbSize != newSPS->maxDpbSize))
  317. {
  318. pStorage->dpb->flushed = 0;
  319. }
  320. else
  321. {
  322. h264bsdFlushDpb(pStorage->dpb);
  323. }
  324. pStorage->oldSpsId = pStorage->activeSpsId;
  325. return(H264BSD_HDRS_RDY);
  326. }
  327. }
  328. /* return error if second phase of
  329. * initialization is not completed */
  330. if (pStorage->pendingActivation)
  331. {
  332. EPRINT("Pending activation not completed");
  333. return (H264BSD_ERROR);
  334. }
  335. tmp = h264bsdDecodeSliceHeader(&strm, pStorage->sliceHeader + 1,
  336. pStorage->activeSps, pStorage->activePps, &nalUnit);
  337. if (tmp != HANTRO_OK)
  338. {
  339. EPRINT("SLICE_HEADER");
  340. return(H264BSD_ERROR);
  341. }
  342. if (h264bsdIsStartOfPicture(pStorage))
  343. {
  344. if (!IS_IDR_NAL_UNIT(&nalUnit))
  345. {
  346. tmp = h264bsdCheckGapsInFrameNum(pStorage->dpb,
  347. pStorage->sliceHeader[1].frameNum,
  348. nalUnit.nalRefIdc != 0 ?
  349. HANTRO_TRUE : HANTRO_FALSE,
  350. pStorage->activeSps->
  351. gapsInFrameNumValueAllowedFlag);
  352. if (tmp != HANTRO_OK)
  353. {
  354. EPRINT("Gaps in frame num");
  355. return(H264BSD_ERROR);
  356. }
  357. }
  358. pStorage->currImage->data =
  359. h264bsdAllocateDpbImage(pStorage->dpb);
  360. }
  361. /* store slice header to storage if successfully decoded */
  362. pStorage->sliceHeader[0] = pStorage->sliceHeader[1];
  363. pStorage->validSliceInAccessUnit = HANTRO_TRUE;
  364. pStorage->prevNalUnit[0] = nalUnit;
  365. h264bsdComputeSliceGroupMap(pStorage,
  366. pStorage->sliceHeader->sliceGroupChangeCycle);
  367. h264bsdInitRefPicList(pStorage->dpb);
  368. tmp = h264bsdReorderRefPicList(pStorage->dpb,
  369. &pStorage->sliceHeader->refPicListReordering,
  370. pStorage->sliceHeader->frameNum,
  371. pStorage->sliceHeader->numRefIdxL0Active);
  372. if (tmp != HANTRO_OK)
  373. {
  374. EPRINT("Reordering");
  375. return(H264BSD_ERROR);
  376. }
  377. DEBUG(("SLICE DATA, FIRST %d\n",
  378. pStorage->sliceHeader->firstMbInSlice));
  379. tmp = h264bsdDecodeSliceData(&strm, pStorage,
  380. pStorage->currImage, pStorage->sliceHeader);
  381. if (tmp != HANTRO_OK)
  382. {
  383. EPRINT("SLICE_DATA");
  384. h264bsdMarkSliceCorrupted(pStorage,
  385. pStorage->sliceHeader->firstMbInSlice);
  386. return(H264BSD_ERROR);
  387. }
  388. if (h264bsdIsEndOfPicture(pStorage))
  389. {
  390. picReady = HANTRO_TRUE;
  391. pStorage->skipRedundantSlices = HANTRO_TRUE;
  392. }
  393. break;
  394. case NAL_SEI:
  395. DEBUG(("SEI MESSAGE, NOT DECODED"));
  396. break;
  397. default:
  398. DEBUG(("NOT IMPLEMENTED YET %d\n",nalUnit.nalUnitType));
  399. }
  400. }
  401. if (picReady)
  402. {
  403. h264bsdFilterPicture(pStorage->currImage, pStorage->mb);
  404. h264bsdResetStorage(pStorage);
  405. picOrderCnt = h264bsdDecodePicOrderCnt(pStorage->poc,
  406. pStorage->activeSps, pStorage->sliceHeader, pStorage->prevNalUnit);
  407. if (pStorage->validSliceInAccessUnit)
  408. {
  409. if (pStorage->prevNalUnit->nalRefIdc)
  410. {
  411. tmp = h264bsdMarkDecRefPic(pStorage->dpb,
  412. &pStorage->sliceHeader->decRefPicMarking,
  413. pStorage->currImage, pStorage->sliceHeader->frameNum,
  414. picOrderCnt,
  415. IS_IDR_NAL_UNIT(pStorage->prevNalUnit) ?
  416. HANTRO_TRUE : HANTRO_FALSE,
  417. pStorage->currentPicId, pStorage->numConcealedMbs);
  418. }
  419. /* non-reference picture, just store for possible display
  420. * reordering */
  421. else
  422. {
  423. tmp = h264bsdMarkDecRefPic(pStorage->dpb, NULL,
  424. pStorage->currImage, pStorage->sliceHeader->frameNum,
  425. picOrderCnt,
  426. IS_IDR_NAL_UNIT(pStorage->prevNalUnit) ?
  427. HANTRO_TRUE : HANTRO_FALSE,
  428. pStorage->currentPicId, pStorage->numConcealedMbs);
  429. }
  430. }
  431. pStorage->picStarted = HANTRO_FALSE;
  432. pStorage->validSliceInAccessUnit = HANTRO_FALSE;
  433. return(H264BSD_PIC_RDY);
  434. }
  435. else
  436. return(H264BSD_RDY);
  437. }
  438. /*------------------------------------------------------------------------------
  439. Function: h264bsdShutdown
  440. Functional description:
  441. Shutdown a decoder instance. Function frees all the memories
  442. allocated for the decoder instance.
  443. Inputs:
  444. pStorage pointer to storage data structure
  445. Returns:
  446. none
  447. ------------------------------------------------------------------------------*/
  448. void h264bsdShutdown(storage_t *pStorage)
  449. {
  450. /* Variables */
  451. u32 i;
  452. /* Code */
  453. ASSERT(pStorage);
  454. for (i = 0; i < MAX_NUM_SEQ_PARAM_SETS; i++)
  455. {
  456. if (pStorage->sps[i])
  457. {
  458. FREE(pStorage->sps[i]->offsetForRefFrame);
  459. FREE(pStorage->sps[i]->vuiParameters);
  460. FREE(pStorage->sps[i]);
  461. }
  462. }
  463. for (i = 0; i < MAX_NUM_PIC_PARAM_SETS; i++)
  464. {
  465. if (pStorage->pps[i])
  466. {
  467. FREE(pStorage->pps[i]->runLength);
  468. FREE(pStorage->pps[i]->topLeft);
  469. FREE(pStorage->pps[i]->bottomRight);
  470. FREE(pStorage->pps[i]->sliceGroupId);
  471. FREE(pStorage->pps[i]);
  472. }
  473. }
  474. FREE(pStorage->mbLayer);
  475. FREE(pStorage->mb);
  476. FREE(pStorage->sliceGroupMap);
  477. h264bsdFreeDpb(pStorage->dpb);
  478. }
  479. /*------------------------------------------------------------------------------
  480. Function: h264bsdNextOutputPicture
  481. Functional description:
  482. Get next output picture in display order.
  483. Inputs:
  484. pStorage pointer to storage data structure
  485. Outputs:
  486. picId identifier of the picture will be stored here
  487. isIdrPic IDR flag of the picture will be stored here
  488. numErrMbs number of concealed macroblocks in the picture
  489. will be stored here
  490. Returns:
  491. pointer to the picture data
  492. NULL if no pictures available for display
  493. ------------------------------------------------------------------------------*/
  494. u8* h264bsdNextOutputPicture(storage_t *pStorage, u32 *picId, u32 *isIdrPic,
  495. u32 *numErrMbs)
  496. {
  497. /* Variables */
  498. dpbOutPicture_t *pOut;
  499. /* Code */
  500. ASSERT(pStorage);
  501. pOut = h264bsdDpbOutputPicture(pStorage->dpb);
  502. if (pOut != NULL)
  503. {
  504. *picId = pOut->picId;
  505. *isIdrPic = pOut->isIdr;
  506. *numErrMbs = pOut->numErrMbs;
  507. return (pOut->data);
  508. }
  509. else
  510. return(NULL);
  511. }
  512. /*------------------------------------------------------------------------------
  513. Function: h264bsdPicWidth
  514. Functional description:
  515. Get width of the picture in macroblocks
  516. Inputs:
  517. pStorage pointer to storage data structure
  518. Outputs:
  519. none
  520. Returns:
  521. picture width
  522. 0 if parameters sets not yet activated
  523. ------------------------------------------------------------------------------*/
  524. u32 h264bsdPicWidth(storage_t *pStorage)
  525. {
  526. /* Variables */
  527. /* Code */
  528. ASSERT(pStorage);
  529. if (pStorage->activeSps)
  530. return(pStorage->activeSps->picWidthInMbs);
  531. else
  532. return(0);
  533. }
  534. /*------------------------------------------------------------------------------
  535. Function: h264bsdPicHeight
  536. Functional description:
  537. Get height of the picture in macroblocks
  538. Inputs:
  539. pStorage pointer to storage data structure
  540. Outputs:
  541. none
  542. Returns:
  543. picture width
  544. 0 if parameters sets not yet activated
  545. ------------------------------------------------------------------------------*/
  546. u32 h264bsdPicHeight(storage_t *pStorage)
  547. {
  548. /* Variables */
  549. /* Code */
  550. ASSERT(pStorage);
  551. if (pStorage->activeSps)
  552. return(pStorage->activeSps->picHeightInMbs);
  553. else
  554. return(0);
  555. }
  556. /*------------------------------------------------------------------------------
  557. Function: h264bsdFlushBuffer
  558. Functional description:
  559. Flush the decoded picture buffer, see dpb.c for details
  560. Inputs:
  561. pStorage pointer to storage data structure
  562. ------------------------------------------------------------------------------*/
  563. void h264bsdFlushBuffer(storage_t *pStorage)
  564. {
  565. /* Variables */
  566. /* Code */
  567. ASSERT(pStorage);
  568. h264bsdFlushDpb(pStorage->dpb);
  569. }
  570. /*------------------------------------------------------------------------------
  571. Function: h264bsdCheckValidParamSets
  572. Functional description:
  573. Check if any valid parameter set combinations (SPS/PPS) exists.
  574. Inputs:
  575. pStorage pointer to storage structure
  576. Returns:
  577. 1 at least one valid SPS/PPS combination found
  578. 0 no valid param set combinations found
  579. ------------------------------------------------------------------------------*/
  580. u32 h264bsdCheckValidParamSets(storage_t *pStorage)
  581. {
  582. /* Variables */
  583. /* Code */
  584. ASSERT(pStorage);
  585. return(h264bsdValidParamSets(pStorage) == HANTRO_OK ? 1 : 0);
  586. }
  587. /*------------------------------------------------------------------------------
  588. Function: h264bsdVideoRange
  589. Functional description:
  590. Get value of video_full_range_flag received in the VUI data.
  591. Inputs:
  592. pStorage pointer to storage structure
  593. Returns:
  594. 1 video_full_range_flag received and value is 1
  595. 0 otherwise
  596. ------------------------------------------------------------------------------*/
  597. u32 h264bsdVideoRange(storage_t *pStorage)
  598. {
  599. /* Variables */
  600. /* Code */
  601. ASSERT(pStorage);
  602. if (pStorage->activeSps && pStorage->activeSps->vuiParametersPresentFlag &&
  603. pStorage->activeSps->vuiParameters &&
  604. pStorage->activeSps->vuiParameters->videoSignalTypePresentFlag &&
  605. pStorage->activeSps->vuiParameters->videoFullRangeFlag)
  606. return(1);
  607. else /* default value of video_full_range_flag is 0 */
  608. return(0);
  609. }
  610. /*------------------------------------------------------------------------------
  611. Function: h264bsdMatrixCoefficients
  612. Functional description:
  613. Get value of matrix_coefficients received in the VUI data
  614. Inputs:
  615. pStorage pointer to storage structure
  616. Outputs:
  617. value of matrix_coefficients if received
  618. 2 otherwise (this is the default value)
  619. ------------------------------------------------------------------------------*/
  620. u32 h264bsdMatrixCoefficients(storage_t *pStorage)
  621. {
  622. /* Variables */
  623. /* Code */
  624. ASSERT(pStorage);
  625. if (pStorage->activeSps && pStorage->activeSps->vuiParametersPresentFlag &&
  626. pStorage->activeSps->vuiParameters &&
  627. pStorage->activeSps->vuiParameters->videoSignalTypePresentFlag &&
  628. pStorage->activeSps->vuiParameters->colourDescriptionPresentFlag)
  629. return(pStorage->activeSps->vuiParameters->matrixCoefficients);
  630. else /* default unspecified */
  631. return(2);
  632. }
  633. /*------------------------------------------------------------------------------
  634. Function: hh264bsdCroppingParams
  635. Functional description:
  636. Get cropping parameters of the active SPS
  637. Inputs:
  638. pStorage pointer to storage structure
  639. Outputs:
  640. croppingFlag flag indicating if cropping params present is
  641. stored here
  642. leftOffset cropping left offset in pixels is stored here
  643. width width of the image after cropping is stored here
  644. topOffset cropping top offset in pixels is stored here
  645. height height of the image after cropping is stored here
  646. Returns:
  647. none
  648. ------------------------------------------------------------------------------*/
  649. void h264bsdCroppingParams(storage_t *pStorage, u32 *croppingFlag,
  650. u32 *leftOffset, u32 *width, u32 *topOffset, u32 *height)
  651. {
  652. /* Variables */
  653. /* Code */
  654. ASSERT(pStorage);
  655. if (pStorage->activeSps && pStorage->activeSps->frameCroppingFlag)
  656. {
  657. *croppingFlag = 1;
  658. *leftOffset = 2 * pStorage->activeSps->frameCropLeftOffset;
  659. *width = 16 * pStorage->activeSps->picWidthInMbs -
  660. 2 * (pStorage->activeSps->frameCropLeftOffset +
  661. pStorage->activeSps->frameCropRightOffset);
  662. *topOffset = 2 * pStorage->activeSps->frameCropTopOffset;
  663. *height = 16 * pStorage->activeSps->picHeightInMbs -
  664. 2 * (pStorage->activeSps->frameCropTopOffset +
  665. pStorage->activeSps->frameCropBottomOffset);
  666. }
  667. else
  668. {
  669. *croppingFlag = 0;
  670. *leftOffset = 0;
  671. *width = 0;
  672. *topOffset = 0;
  673. *height = 0;
  674. }
  675. }
  676. /*------------------------------------------------------------------------------
  677. Function: h264bsdSampleAspectRatio
  678. Functional description:
  679. Get aspect ratio received in the VUI data
  680. Inputs:
  681. pStorage pointer to storage structure
  682. Outputs:
  683. sarWidth sample aspect ratio height
  684. sarHeight sample aspect ratio width
  685. ------------------------------------------------------------------------------*/
  686. void h264bsdSampleAspectRatio(storage_t *pStorage, u32 *sarWidth, u32 *sarHeight)
  687. {
  688. /* Variables */
  689. u32 w = 1;
  690. u32 h = 1;
  691. /* Code */
  692. ASSERT(pStorage);
  693. if (pStorage->activeSps &&
  694. pStorage->activeSps->vuiParametersPresentFlag &&
  695. pStorage->activeSps->vuiParameters &&
  696. pStorage->activeSps->vuiParameters->aspectRatioPresentFlag )
  697. {
  698. switch (pStorage->activeSps->vuiParameters->aspectRatioIdc)
  699. {
  700. case ASPECT_RATIO_UNSPECIFIED: w = 0; h = 0; break;
  701. case ASPECT_RATIO_1_1: w = 1; h = 1; break;
  702. case ASPECT_RATIO_12_11: w = 12; h = 11; break;
  703. case ASPECT_RATIO_10_11: w = 10; h = 11; break;
  704. case ASPECT_RATIO_16_11: w = 16; h = 11; break;
  705. case ASPECT_RATIO_40_33: w = 40; h = 33; break;
  706. case ASPECT_RATIO_24_11: w = 24; h = 11; break;
  707. case ASPECT_RATIO_20_11: w = 20; h = 11; break;
  708. case ASPECT_RATIO_32_11: w = 32; h = 11; break;
  709. case ASPECT_RATIO_80_33: w = 80; h = 33; break;
  710. case ASPECT_RATIO_18_11: w = 18; h = 11; break;
  711. case ASPECT_RATIO_15_11: w = 15; h = 11; break;
  712. case ASPECT_RATIO_64_33: w = 64; h = 33; break;
  713. case ASPECT_RATIO_160_99: w = 160; h = 99; break;
  714. case ASPECT_RATIO_EXTENDED_SAR:
  715. w = pStorage->activeSps->vuiParameters->sarWidth;
  716. h = pStorage->activeSps->vuiParameters->sarHeight;
  717. if ((w == 0) || (h == 0))
  718. w = h = 0;
  719. break;
  720. default:
  721. w = 0;
  722. h = 0;
  723. break;
  724. }
  725. }
  726. /* set aspect ratio*/
  727. *sarWidth = w;
  728. *sarHeight = h;
  729. }
  730. /*------------------------------------------------------------------------------
  731. Function: h264bsdProfile
  732. Functional description:
  733. Get profile information from active SPS
  734. Inputs:
  735. pStorage pointer to storage structure
  736. Outputs:
  737. profile current profile
  738. ------------------------------------------------------------------------------*/
  739. u32 h264bsdProfile(storage_t *pStorage)
  740. {
  741. if (pStorage->activeSps)
  742. return pStorage->activeSps->profileIdc;
  743. else
  744. return 0;
  745. }