/Avc/avc_bitstream.cpp

http://github.com/mbebenita/Broadway · C++ · 276 lines · 145 code · 39 blank · 92 comment · 15 complexity · 430cd17351d8cf78566def824824b68b MD5 · raw file

  1. /* ------------------------------------------------------------------
  2. * Copyright (C) 1998-2009 PacketVideo
  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
  13. * express or implied.
  14. * See the License for the specific language governing permissions
  15. * and limitations under the License.
  16. * -------------------------------------------------------------------
  17. */
  18. #include "avcdec_bitstream.h"
  19. /* Swapping may not be needed anymore since we read one byte at a time and perform
  20. EBSP to RBSP conversion in bitstream. */
  21. #ifdef LITTLE_ENDIAN
  22. #if (WORD_SIZE==32) /* this can be replaced with assembly instructions */
  23. #define SWAP_BYTES(x) ((((x)&0xFF)<<24) | (((x)&0xFF00)<<8) | (((x)&0xFF0000)>>8) | (((x)&0xFF000000)>>24))
  24. #else /* for 16-bit */
  25. #define SWAP_BYTES(x) ((((x)&0xFF)<<8) | (((x)&0xFF00)>>8))
  26. #endif
  27. #else
  28. #define SWAP_BYTES(x) (x)
  29. #endif
  30. /* array for trailing bit pattern as function of number of bits */
  31. /* the first one is unused. */
  32. const static uint8 trailing_bits[9] = {0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
  33. /* ======================================================================== */
  34. /* Function : BitstreamInit() */
  35. /* Date : 11/4/2003 */
  36. /* Purpose : Populate bitstream structure with bitstream buffer and size */
  37. /* it also initializes internal data */
  38. /* In/out : */
  39. /* Return : AVCDEC_SUCCESS if successed, AVCDEC_FAIL if failed. */
  40. /* Modified : */
  41. /* ======================================================================== */
  42. /* |--------|--------|----~~~~~-----|---------|---------|---------|
  43. ^ ^read_pos ^data_end_pos
  44. bitstreamBuffer <--------->
  45. current_word
  46. |xxxxxxxxxxxxx----| = current_word 32 or 16 bits
  47. <------------>
  48. bit_left
  49. ======================================================================== */
  50. /* ======================================================================== */
  51. /* Function : BitstreamNextWord() */
  52. /* Date : 12/4/2003 */
  53. /* Purpose : Read up to machine word. */
  54. /* In/out : */
  55. /* Return : Next word with emulation prevention code removed. Everything
  56. in the bitstream structure got modified except current_word */
  57. /* Modified : */
  58. /* ======================================================================== */
  59. AVCDec_Status BitstreamInit(AVCDecBitstream *stream, uint8 *buffer, int size)
  60. {
  61. EBSPtoRBSP(buffer, &size);
  62. stream->incnt = 0;
  63. stream->incnt_next = 0;
  64. stream->bitcnt = 0;
  65. stream->curr_word = stream->next_word = 0;
  66. stream->read_pos = 0;
  67. stream->bitstreamBuffer = buffer;
  68. stream->data_end_pos = size;
  69. stream->nal_size = size;
  70. return AVCDEC_SUCCESS;
  71. }
  72. /* ======================================================================== */
  73. /* Function : AVC_BitstreamFillCache() */
  74. /* Date : 1/1/2005 */
  75. /* Purpose : Read up to machine word. */
  76. /* In/out : */
  77. /* Return : Read in 4 bytes of input data */
  78. /* Modified : */
  79. /* ======================================================================== */
  80. AVCDec_Status AVC_BitstreamFillCache(AVCDecBitstream *stream)
  81. {
  82. uint8 *bitstreamBuffer = stream->bitstreamBuffer;
  83. uint8 *v;
  84. int num_bits, i;
  85. stream->curr_word |= (stream->next_word >> stream->incnt); // stream->incnt cannot be 32
  86. stream->next_word <<= (31 - stream->incnt);
  87. stream->next_word <<= 1;
  88. num_bits = stream->incnt_next + stream->incnt;
  89. if (num_bits >= 32)
  90. {
  91. stream->incnt_next -= (32 - stream->incnt);
  92. stream->incnt = 32;
  93. return AVCDEC_SUCCESS;
  94. }
  95. /* this check can be removed if there is additional extra 4 bytes at the end of the bitstream */
  96. v = bitstreamBuffer + stream->read_pos;
  97. if (stream->read_pos > stream->data_end_pos - 4)
  98. {
  99. if (stream->data_end_pos <= stream->read_pos)
  100. {
  101. stream->incnt = num_bits;
  102. stream->incnt_next = 0;
  103. return AVCDEC_SUCCESS;
  104. }
  105. stream->next_word = 0;
  106. for (i = 0; i < stream->data_end_pos - stream->read_pos; i++)
  107. {
  108. stream->next_word |= (v[i] << ((3 - i) << 3));
  109. }
  110. stream->read_pos = stream->data_end_pos;
  111. stream->curr_word |= (stream->next_word >> num_bits); // this is safe
  112. stream->next_word <<= (31 - num_bits);
  113. stream->next_word <<= 1;
  114. num_bits = i << 3;
  115. stream->incnt += stream->incnt_next;
  116. stream->incnt_next = num_bits - (32 - stream->incnt);
  117. if (stream->incnt_next < 0)
  118. {
  119. stream->incnt += num_bits;
  120. stream->incnt_next = 0;
  121. }
  122. else
  123. {
  124. stream->incnt = 32;
  125. }
  126. return AVCDEC_SUCCESS;
  127. }
  128. stream->next_word = ((uint32)v[0] << 24) | (v[1] << 16) | (v[2] << 8) | v[3];
  129. stream->read_pos += 4;
  130. stream->curr_word |= (stream->next_word >> num_bits); // this is safe
  131. stream->next_word <<= (31 - num_bits);
  132. stream->next_word <<= 1;
  133. stream->incnt_next += stream->incnt;
  134. stream->incnt = 32;
  135. return AVCDEC_SUCCESS;
  136. }
  137. /* ======================================================================== */
  138. /* Function : BitstreamReadBits() */
  139. /* Date : 11/4/2003 */
  140. /* Purpose : Read up to machine word. */
  141. /* In/out : */
  142. /* Return : AVCDEC_SUCCESS if successed, AVCDEC_FAIL if number of bits */
  143. /* is greater than the word-size, AVCDEC_PACKET_LOSS or */
  144. /* AVCDEC_NO_DATA if callback to get data fails. */
  145. /* Modified : */
  146. /* ======================================================================== */
  147. AVCDec_Status BitstreamReadBits(AVCDecBitstream *stream, int nBits, uint *code)
  148. {
  149. if (stream->incnt < nBits)
  150. {
  151. /* frame-based decoding */
  152. AVC_BitstreamFillCache(stream);
  153. }
  154. *code = stream->curr_word >> (32 - nBits);
  155. BitstreamFlushBits(stream, nBits);
  156. return AVCDEC_SUCCESS;
  157. }
  158. /* ======================================================================== */
  159. /* Function : BitstreamShowBits() */
  160. /* Date : 11/4/2003 */
  161. /* Purpose : Show up to machine word without advancing the pointer. */
  162. /* In/out : */
  163. /* Return : AVCDEC_SUCCESS if successed, AVCDEC_FAIL if number of bits */
  164. /* is greater than the word-size, AVCDEC_NO_DATA if it needs */
  165. /* to callback to get data. */
  166. /* Modified : */
  167. /* ======================================================================== */
  168. AVCDec_Status BitstreamShowBits(AVCDecBitstream *stream, int nBits, uint *code)
  169. {
  170. if (stream->incnt < nBits)
  171. {
  172. /* frame-based decoding */
  173. AVC_BitstreamFillCache(stream);
  174. }
  175. *code = stream->curr_word >> (32 - nBits);
  176. return AVCDEC_SUCCESS;
  177. }
  178. /* ======================================================================== */
  179. /* Function : BitstreamRead1Bit() */
  180. /* Date : 11/4/2003 */
  181. /* Purpose : Read 1 bit from the bitstream. */
  182. /* In/out : */
  183. /* Return : AVCDEC_SUCCESS if successed, AVCDEC_FAIL if number of bits */
  184. /* is greater than the word-size, AVCDEC_PACKET_LOSS or */
  185. /* AVCDEC_NO_DATA if callback to get data fails. */
  186. /* Modified : */
  187. /* ======================================================================== */
  188. AVCDec_Status BitstreamRead1Bit(AVCDecBitstream *stream, uint *code)
  189. {
  190. if (stream->incnt < 1)
  191. {
  192. /* frame-based decoding */
  193. AVC_BitstreamFillCache(stream);
  194. }
  195. *code = stream->curr_word >> 31;
  196. BitstreamFlushBits(stream, 1);
  197. return AVCDEC_SUCCESS;
  198. }
  199. AVCDec_Status BitstreamByteAlign(AVCDecBitstream *stream)
  200. {
  201. uint n_stuffed;
  202. n_stuffed = (8 - (stream->bitcnt & 0x7)) & 0x7; /* 07/05/01 */
  203. stream->bitcnt += n_stuffed;
  204. stream->incnt -= n_stuffed;
  205. if (stream->incnt < 0)
  206. {
  207. stream->bitcnt += stream->incnt;
  208. stream->incnt = 0;
  209. }
  210. stream->curr_word <<= n_stuffed;
  211. return AVCDEC_SUCCESS;
  212. }
  213. /* check whether there are more RBSP data. */
  214. /* ignore the emulation prevention code, assume it has been taken out. */
  215. bool more_rbsp_data(AVCDecBitstream *stream)
  216. {
  217. int total_bit_left;
  218. uint code;
  219. if (stream->read_pos >= stream->nal_size)
  220. {
  221. total_bit_left = stream->incnt_next + stream->incnt;
  222. if (total_bit_left <= 0)
  223. {
  224. return FALSE;
  225. }
  226. else if (total_bit_left <= 8)
  227. {
  228. BitstreamShowBits(stream, total_bit_left, &code);
  229. if (code == trailing_bits[total_bit_left])
  230. {
  231. return FALSE;
  232. }
  233. }
  234. }
  235. return TRUE;
  236. }