You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
8.6KB

  1. /*
  2. * Copyright (c) 2016 Alexandra Hájková
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * functions for reading bits from a buffer
  23. */
  24. #ifndef AVCODEC_BITSTREAM_H
  25. #define AVCODEC_BITSTREAM_H
  26. #include <stdint.h>
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/log.h"
  30. #include "mathops.h"
  31. typedef struct BitstreamContext {
  32. uint64_t bits; // stores bits read from the buffer
  33. const uint8_t *buffer, *buffer_end;
  34. const uint8_t *ptr; // position inside a buffer
  35. unsigned bits_left; // number of bits left in bits field
  36. unsigned size_in_bits;
  37. } BitstreamContext;
  38. static inline void refill_64(BitstreamContext *bc)
  39. {
  40. if (bc->ptr >= bc->buffer_end)
  41. return;
  42. #ifdef BITSTREAM_READER_LE
  43. bc->bits = AV_RL64(bc->ptr);
  44. #else
  45. bc->bits = AV_RB64(bc->ptr);
  46. #endif
  47. bc->ptr += 8;
  48. bc->bits_left = 64;
  49. }
  50. static inline void refill_32(BitstreamContext *bc)
  51. {
  52. if (bc->ptr >= bc->buffer_end)
  53. return;
  54. #ifdef BITSTREAM_READER_LE
  55. bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;
  56. #else
  57. bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);
  58. #endif
  59. bc->ptr += 4;
  60. bc->bits_left += 32;
  61. }
  62. /* Initialize BitstreamContext. Input buffer must have an additional zero
  63. * padding of AV_INPUT_BUFFER_PADDING_SIZE bytes at the end. */
  64. static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,
  65. unsigned bit_size)
  66. {
  67. unsigned buffer_size;
  68. if (bit_size > INT_MAX - 7 || !buffer) {
  69. buffer =
  70. bc->buffer =
  71. bc->ptr = NULL;
  72. bc->bits_left = 0;
  73. return AVERROR_INVALIDDATA;
  74. }
  75. buffer_size = (bit_size + 7) >> 3;
  76. bc->buffer = buffer;
  77. bc->buffer_end = buffer + buffer_size;
  78. bc->ptr = bc->buffer;
  79. bc->size_in_bits = bit_size;
  80. bc->bits_left = 0;
  81. bc->bits = 0;
  82. refill_64(bc);
  83. return 0;
  84. }
  85. /* Initialize BitstreamContext with buffer size in bytes instead of bits. */
  86. static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,
  87. unsigned byte_size)
  88. {
  89. if (byte_size > INT_MAX / 8)
  90. return AVERROR_INVALIDDATA;
  91. return bitstream_init(bc, buffer, byte_size * 8);
  92. }
  93. /* Return number of bits already read. */
  94. static inline int bitstream_tell(const BitstreamContext *bc)
  95. {
  96. return (bc->ptr - bc->buffer) * 8 - bc->bits_left;
  97. }
  98. /* Return buffer size in bits. */
  99. static inline int bitstream_tell_size(const BitstreamContext *bc)
  100. {
  101. return bc->size_in_bits;
  102. }
  103. /* Return the number of the bits left in a buffer. */
  104. static inline int bitstream_bits_left(const BitstreamContext *bc)
  105. {
  106. return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_left;
  107. }
  108. static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
  109. {
  110. #ifdef BITSTREAM_READER_LE
  111. uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
  112. bc->bits >>= n;
  113. #else
  114. uint64_t ret = bc->bits >> (64 - n);
  115. bc->bits <<= n;
  116. #endif
  117. bc->bits_left -= n;
  118. return ret;
  119. }
  120. /* Return one bit from the buffer. */
  121. static inline unsigned bitstream_read_bit(BitstreamContext *bc)
  122. {
  123. if (!bc->bits_left)
  124. refill_64(bc);
  125. return get_val(bc, 1);
  126. }
  127. /* Return n bits from the buffer. n has to be in the 0-63 range. */
  128. static inline uint64_t bitstream_read_63(BitstreamContext *bc, unsigned n)
  129. {
  130. uint64_t ret = 0;
  131. #ifdef BITSTREAM_READER_LE
  132. uint64_t left = 0;
  133. #endif
  134. if (!n)
  135. return 0;
  136. if (n > bc->bits_left) {
  137. n -= bc->bits_left;
  138. #ifdef BITSTREAM_READER_LE
  139. left = bc->bits_left;
  140. #endif
  141. ret = get_val(bc, bc->bits_left);
  142. refill_64(bc);
  143. }
  144. #ifdef BITSTREAM_READER_LE
  145. ret = get_val(bc, n) << left | ret;
  146. #else
  147. ret = get_val(bc, n) | ret << n;
  148. #endif
  149. return ret;
  150. }
  151. /* Return n bits from the buffer. n has to be in the 0-32 range. */
  152. static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)
  153. {
  154. if (!n)
  155. return 0;
  156. if (n > bc->bits_left) {
  157. refill_32(bc);
  158. if (bc->bits_left < 32)
  159. bc->bits_left = n;
  160. }
  161. return get_val(bc, n);
  162. }
  163. /* Return n bits from the buffer as a signed integer.
  164. * n has to be in the 0-32 range. */
  165. static inline int32_t bitstream_read_signed(BitstreamContext *bc, unsigned n)
  166. {
  167. return sign_extend(bitstream_read(bc, n), n);
  168. }
  169. static inline unsigned show_val(const BitstreamContext *bc, unsigned n)
  170. {
  171. #ifdef BITSTREAM_READER_LE
  172. return bc->bits & ((UINT64_C(1) << n) - 1);
  173. #else
  174. return bc->bits >> (64 - n);
  175. #endif
  176. }
  177. /* Return n bits from the buffer, but do not change the buffer state.
  178. * n has to be in the 0-32 range. */
  179. static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)
  180. {
  181. if (n > bc->bits_left)
  182. refill_32(bc);
  183. return show_val(bc, n);
  184. }
  185. /* Return n bits from the buffer as a signed integer, but do not change the
  186. * buffer state. n has to be in the 0-32 range. */
  187. static inline int bitstream_peek_signed(BitstreamContext *bc, unsigned n)
  188. {
  189. return sign_extend(bitstream_peek(bc, n), n);
  190. }
  191. static inline void skip_remaining(BitstreamContext *bc, unsigned n)
  192. {
  193. #ifdef BITSTREAM_READER_LE
  194. bc->bits >>= n;
  195. #else
  196. bc->bits <<= n;
  197. #endif
  198. bc->bits_left -= n;
  199. }
  200. /* Skip n bits in the buffer. */
  201. static inline void bitstream_skip(BitstreamContext *bc, unsigned n)
  202. {
  203. if (n < bc->bits_left)
  204. skip_remaining(bc, n);
  205. else {
  206. n -= bc->bits_left;
  207. bc->bits = 0;
  208. bc->bits_left = 0;
  209. if (n >= 64) {
  210. unsigned skip = n / 8;
  211. n -= skip * 8;
  212. bc->ptr += skip;
  213. }
  214. refill_64(bc);
  215. if (n)
  216. skip_remaining(bc, n);
  217. }
  218. }
  219. /* Seek to the given bit position. */
  220. static inline void bitstream_seek(BitstreamContext *bc, unsigned pos)
  221. {
  222. bc->ptr = bc->buffer;
  223. bc->bits = 0;
  224. bc->bits_left = 0;
  225. bitstream_skip(bc, pos);
  226. }
  227. /* Skip bits to a byte boundary. */
  228. static inline const uint8_t *bitstream_align(BitstreamContext *bc)
  229. {
  230. unsigned n = -bitstream_tell(bc) & 7;
  231. if (n)
  232. bitstream_skip(bc, n);
  233. return bc->buffer + (bitstream_tell(bc) >> 3);
  234. }
  235. /* Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
  236. * If MSB not set it is negative. */
  237. static inline int bitstream_read_xbits(BitstreamContext *bc, unsigned length)
  238. {
  239. int32_t cache = bitstream_peek(bc, 32);
  240. int sign = ~cache >> 31;
  241. skip_remaining(bc, length);
  242. return ((((uint32_t)(sign ^ cache)) >> (32 - length)) ^ sign) - sign;
  243. }
  244. /* Return decoded truncated unary code for the values 0, 1, 2. */
  245. static inline int bitstream_decode012(BitstreamContext *bc)
  246. {
  247. if (!bitstream_read_bit(bc))
  248. return 0;
  249. else
  250. return bitstream_read_bit(bc) + 1;
  251. }
  252. /* Return decoded truncated unary code for the values 2, 1, 0. */
  253. static inline int bitstream_decode210(BitstreamContext *bc)
  254. {
  255. if (bitstream_read_bit(bc))
  256. return 0;
  257. else
  258. return 2 - bitstream_read_bit(bc);
  259. }
  260. /* Read sign bit and flip the sign of the provided value accordingly. */
  261. static inline int bitstream_apply_sign(BitstreamContext *bc, int val)
  262. {
  263. int sign = bitstream_read_signed(bc, 1);
  264. return (val ^ sign) - sign;
  265. }
  266. /* Unwind the cache so a refill_32 can fill it again. */
  267. static inline void bitstream_unwind(BitstreamContext *bc)
  268. {
  269. int unwind = 4;
  270. int unwind_bits = unwind * 8;
  271. if (bc->bits_left < unwind_bits)
  272. return;
  273. bc->bits >>= unwind_bits;
  274. bc->bits <<= unwind_bits;
  275. bc->bits_left -= unwind_bits;
  276. bc->ptr -= unwind;
  277. }
  278. /* Unget up to 32 bits. */
  279. static inline void bitstream_unget(BitstreamContext *bc, uint64_t value,
  280. size_t amount)
  281. {
  282. size_t cache_size = sizeof(bc->bits) * 8;
  283. if (bc->bits_left + amount > cache_size)
  284. bitstream_unwind(bc);
  285. bc->bits = (bc->bits >> amount) | (value << (cache_size - amount));
  286. bc->bits_left += amount;
  287. }
  288. #endif /* AVCODEC_BITSTREAM_H */