PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/lencod/src/nal.c

https://github.com/c444b774/jm-decision
C | 144 lines | 62 code | 17 blank | 65 comment | 8 complexity | 895b086b954035e592146cfe0f991d61 MD5 | raw file
  1. /*!
  2. **************************************************************************************
  3. * \file
  4. * nal.c
  5. * \brief
  6. * Handles the operations on converting String of Data Bits (SODB)
  7. * to Raw Byte Sequence Payload (RBSP), and then
  8. * onto Encapsulate Byte Sequence Payload (EBSP).
  9. * \date 14 June 2002
  10. * \author
  11. * Main contributors (see contributors.h for copyright, address and affiliation details)
  12. * - Shankar Regunathan <shanre@microsoft.de>
  13. * - Stephan Wenger <stewe@cs.tu-berlin.de>
  14. ***************************************************************************************
  15. */
  16. #include "contributors.h"
  17. #include "global.h"
  18. #include "enc_statistics.h"
  19. #include "biariencode.h"
  20. #include "nal.h"
  21. static const int MbWidthC [4]= { 0, 8, 8, 16};
  22. static const int MbHeightC [4]= { 0, 8, 16, 16};
  23. /*!
  24. ************************************************************************
  25. * \brief
  26. * Converts String Of Data Bits (SODB) to Raw Byte Sequence
  27. * Packet (RBSP)
  28. * \param currStream
  29. * Bitstream which contains data bits.
  30. * \return None
  31. * \note currStream is byte-aligned at the end of this function
  32. *
  33. ************************************************************************
  34. */
  35. void SODBtoRBSP(Bitstream *currStream)
  36. {
  37. currStream->byte_buf <<= 1;
  38. currStream->byte_buf |= 1;
  39. currStream->bits_to_go--;
  40. currStream->byte_buf <<= currStream->bits_to_go;
  41. currStream->streamBuffer[currStream->byte_pos++] = currStream->byte_buf;
  42. currStream->bits_to_go = 8;
  43. currStream->byte_buf = 0;
  44. }
  45. /*!
  46. ************************************************************************
  47. * \brief
  48. * This function add emulation_prevention_three_byte for all occurrences
  49. * of the following byte sequences in the stream
  50. * 0x000000 -> 0x00000300
  51. * 0x000001 -> 0x00000301
  52. * 0x000002 -> 0x00000302
  53. * 0x000003 -> 0x00000303
  54. *
  55. * \param NaluBuffer
  56. * pointer to target buffer
  57. * \param rbsp
  58. * pointer to source buffer
  59. * \param rbsp_size
  60. * Size of source
  61. * \return
  62. * Size target buffer after emulation prevention.
  63. *
  64. ************************************************************************
  65. */
  66. int RBSPtoEBSP(byte *NaluBuffer, unsigned char *rbsp, int rbsp_size)
  67. {
  68. int j = 0;
  69. int count = 0;
  70. int i;
  71. for(i = 0; i < rbsp_size; i++)
  72. {
  73. if(count == ZEROBYTES_SHORTSTARTCODE && !(rbsp[i] & 0xFC))
  74. {
  75. NaluBuffer[j] = 0x03;
  76. j++;
  77. count = 0;
  78. }
  79. NaluBuffer[j] = rbsp[i];
  80. if(rbsp[i] == 0x00)
  81. count++;
  82. else
  83. count = 0;
  84. j++;
  85. }
  86. return j;
  87. }
  88. /*!
  89. ************************************************************************
  90. * \brief
  91. * This function adds cabac_zero_word syntax elements at the end of the
  92. * NAL unit to
  93. *
  94. * \param p_Vid
  95. * VideoParameters structure
  96. * \param nalu
  97. * target NAL unit
  98. * \param cur_stats
  99. * currently used statistics parameters
  100. * \return
  101. * number of added bytes
  102. *
  103. ************************************************************************
  104. */
  105. int addCabacZeroWords(VideoParameters *p_Vid, NALU_t *nalu, StatParameters *cur_stats)
  106. {
  107. seq_parameter_set_rbsp_t *active_sps = p_Vid->active_sps;
  108. int stuffing_bytes = 0;
  109. int i = 0;
  110. byte *buf = &nalu->buf[nalu->len];
  111. int RawMbBits = 256 * p_Vid->bitdepth_luma + 2 * MbWidthC[active_sps->chroma_format_idc] * MbHeightC[active_sps->chroma_format_idc] * p_Vid->bitdepth_chroma;
  112. int min_num_bytes = ((96 * get_pic_bin_count(p_Vid)) - (RawMbBits * (int)p_Vid->PicSizeInMbs *3) + 1023) / 1024;
  113. if (min_num_bytes > p_Vid->bytes_in_picture)
  114. {
  115. stuffing_bytes = min_num_bytes - p_Vid->bytes_in_picture;
  116. printf ("Inserting %d/%d cabac_zero_word syntax elements/bytes (Clause 7.4.2.10)\n", ((stuffing_bytes + 2)/3), stuffing_bytes);
  117. for (i = 0; i < stuffing_bytes; i+=3 )
  118. {
  119. *buf++ = 0x00; // CABAC zero word
  120. *buf++ = 0x00;
  121. *buf++ = 0x03;
  122. }
  123. cur_stats->bit_use_stuffingBits[p_Vid->type] += (i<<3);
  124. nalu->len += i;
  125. }
  126. return i;
  127. }