1
0

nalu.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. ************************************************************************
  3. * \file nalu.c
  4. *
  5. * \brief
  6. * Decoder NALU support functions
  7. *
  8. * \author
  9. * Main contributors (see contributors.h for copyright, address and affiliation details)
  10. * - Stephan Wenger <[email protected]>
  11. ************************************************************************
  12. */
  13. #include "global.h"
  14. #include "nalu.h"
  15. #include "memalloc.h"
  16. #include "meminput.h"
  17. /*!
  18. *************************************************************************************
  19. * \brief
  20. * Initialize bitstream reading structure
  21. *
  22. * \param
  23. * p_Vid: Imageparameter information
  24. * \param
  25. * filemode:
  26. *
  27. *************************************************************************************
  28. */
  29. void OpenMemory(VideoParameters *p_Vid, const char *fn);
  30. void CloseMemory(VideoParameters *p_Vid);
  31. int GetMemoryNALU (VideoParameters *p_Vid, NALU_t *nalu);
  32. void initBitsFile (VideoParameters *p_Vid)
  33. {
  34. malloc_mem_input(p_Vid);
  35. p_Vid->nalu = AllocNALU(MAX_CODED_FRAME_SIZE);
  36. }
  37. /*!
  38. *************************************************************************************
  39. * \brief
  40. * Converts a NALU to an RBSP
  41. *
  42. * \param
  43. * nalu: nalu structure to be filled
  44. *
  45. * \return
  46. * length of the RBSP in bytes
  47. *************************************************************************************
  48. */
  49. static int NALUtoRBSP (NALU_t *nalu)
  50. {
  51. assert (nalu != NULL);
  52. nalu->len = EBSPtoRBSP (nalu->buf, nalu->len) ;
  53. return nalu->len ;
  54. }
  55. /*!
  56. ************************************************************************
  57. * \brief
  58. * Read the next NAL unit (with error handling)
  59. ************************************************************************
  60. */
  61. int read_next_nalu(VideoParameters *p_Vid, NALU_t *nalu)
  62. {
  63. InputParameters *p_Inp = p_Vid->p_Inp;
  64. int ret;
  65. ret = GetMemoryNALU(p_Vid, nalu);
  66. if (ret < 0)
  67. {
  68. error ("Error while getting the next NALU, exit\n", 601);
  69. }
  70. if (ret == 0)
  71. {
  72. return 0;
  73. }
  74. //In some cases, zero_byte shall be present. If current NALU is a VCL NALU, we can't tell
  75. //whether it is the first VCL NALU at this point, so only non-VCL NAL unit is checked here.
  76. CheckZeroByteNonVCL(p_Vid, nalu);
  77. ret = NALUtoRBSP(nalu);
  78. if (ret < 0)
  79. error ("Invalid startcode emulation prevention found.", 602);
  80. // Got a NALU
  81. if (nalu->forbidden_bit)
  82. {
  83. error ("Found NALU with forbidden_bit set, bit error?", 603);
  84. }
  85. return nalu->len;
  86. }
  87. void CheckZeroByteNonVCL(VideoParameters *p_Vid, NALU_t *nalu)
  88. {
  89. int CheckZeroByte=0;
  90. //This function deals only with non-VCL NAL units
  91. if(nalu->nal_unit_type>=1&&nalu->nal_unit_type<=5)
  92. return;
  93. //for SPS and PPS, zero_byte shall exist
  94. if(nalu->nal_unit_type==NALU_TYPE_SPS || nalu->nal_unit_type==NALU_TYPE_PPS)
  95. CheckZeroByte=1;
  96. //check the possibility of the current NALU to be the start of a new access unit, according to 7.4.1.2.3
  97. if(nalu->nal_unit_type==NALU_TYPE_AUD || nalu->nal_unit_type==NALU_TYPE_SPS ||
  98. nalu->nal_unit_type==NALU_TYPE_PPS || nalu->nal_unit_type==NALU_TYPE_SEI ||
  99. (nalu->nal_unit_type>=13 && nalu->nal_unit_type<=18))
  100. {
  101. if(p_Vid->LastAccessUnitExists)
  102. {
  103. p_Vid->LastAccessUnitExists=0; //deliver the last access unit to decoder
  104. p_Vid->NALUCount=0;
  105. }
  106. }
  107. p_Vid->NALUCount++;
  108. //for the first NAL unit in an access unit, zero_byte shall exists
  109. if(p_Vid->NALUCount==1)
  110. CheckZeroByte=1;
  111. if(CheckZeroByte && nalu->startcodeprefix_len==3)
  112. {
  113. // printf("Warning: zero_byte shall exist\n");
  114. //because it is not a very serious problem, we do not exit here
  115. }
  116. }
  117. void CheckZeroByteVCL(VideoParameters *p_Vid, NALU_t *nalu)
  118. {
  119. int CheckZeroByte=0;
  120. //This function deals only with VCL NAL units
  121. if(!(nalu->nal_unit_type>=1&&nalu->nal_unit_type<=5))
  122. return;
  123. if(p_Vid->LastAccessUnitExists)
  124. {
  125. p_Vid->NALUCount=0;
  126. }
  127. p_Vid->NALUCount++;
  128. //the first VCL NAL unit that is the first NAL unit after last VCL NAL unit indicates
  129. //the start of a new access unit and hence the first NAL unit of the new access unit. (sounds like a tongue twister :-)
  130. if(p_Vid->NALUCount == 1)
  131. CheckZeroByte = 1;
  132. p_Vid->LastAccessUnitExists = 1;
  133. if(CheckZeroByte && nalu->startcodeprefix_len==3)
  134. {
  135. //printf("warning: zero_byte shall exist\n");
  136. //because it is not a very serious problem, we do not exit here
  137. }
  138. }