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.

416 lines
12KB

  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. #include "vlc.h"
  32. typedef struct BitstreamContext {
  33. uint64_t bits; // stores bits read from the buffer
  34. const uint8_t *buffer, *buffer_end;
  35. const uint8_t *ptr; // position inside a buffer
  36. unsigned bits_left; // number of bits left in bits field
  37. unsigned size_in_bits;
  38. } BitstreamContext;
  39. static inline void refill_64(BitstreamContext *bc)
  40. {
  41. if (bc->ptr >= bc->buffer_end)
  42. return;
  43. #ifdef BITSTREAM_READER_LE
  44. bc->bits = AV_RL64(bc->ptr);
  45. #else
  46. bc->bits = AV_RB64(bc->ptr);
  47. #endif
  48. bc->ptr += 8;
  49. bc->bits_left = 64;
  50. }
  51. static inline void refill_32(BitstreamContext *bc)
  52. {
  53. if (bc->ptr >= bc->buffer_end)
  54. return;
  55. #ifdef BITSTREAM_READER_LE
  56. bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;
  57. #else
  58. bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);
  59. #endif
  60. bc->ptr += 4;
  61. bc->bits_left += 32;
  62. }
  63. /* Initialize BitstreamContext. Input buffer must have an additional zero
  64. * padding of AV_INPUT_BUFFER_PADDING_SIZE bytes at the end. */
  65. static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,
  66. unsigned bit_size)
  67. {
  68. unsigned buffer_size;
  69. if (bit_size > INT_MAX - 7 || !buffer) {
  70. buffer =
  71. bc->buffer =
  72. bc->ptr = NULL;
  73. bc->bits_left = 0;
  74. return AVERROR_INVALIDDATA;
  75. }
  76. buffer_size = (bit_size + 7) >> 3;
  77. bc->buffer = buffer;
  78. bc->buffer_end = buffer + buffer_size;
  79. bc->ptr = bc->buffer;
  80. bc->size_in_bits = bit_size;
  81. bc->bits_left = 0;
  82. bc->bits = 0;
  83. refill_64(bc);
  84. return 0;
  85. }
  86. /* Initialize BitstreamContext with buffer size in bytes instead of bits. */
  87. static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,
  88. unsigned byte_size)
  89. {
  90. if (byte_size > INT_MAX / 8)
  91. return AVERROR_INVALIDDATA;
  92. return bitstream_init(bc, buffer, byte_size * 8);
  93. }
  94. /* Return number of bits already read. */
  95. static inline int bitstream_tell(const BitstreamContext *bc)
  96. {
  97. return (bc->ptr - bc->buffer) * 8 - bc->bits_left;
  98. }
  99. /* Return buffer size in bits. */
  100. static inline int bitstream_tell_size(const BitstreamContext *bc)
  101. {
  102. return bc->size_in_bits;
  103. }
  104. /* Return the number of the bits left in a buffer. */
  105. static inline int bitstream_bits_left(const BitstreamContext *bc)
  106. {
  107. return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_left;
  108. }
  109. static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
  110. {
  111. #ifdef BITSTREAM_READER_LE
  112. uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
  113. bc->bits >>= n;
  114. #else
  115. uint64_t ret = bc->bits >> (64 - n);
  116. bc->bits <<= n;
  117. #endif
  118. bc->bits_left -= n;
  119. return ret;
  120. }
  121. /* Return one bit from the buffer. */
  122. static inline unsigned bitstream_read_bit(BitstreamContext *bc)
  123. {
  124. if (!bc->bits_left)
  125. refill_64(bc);
  126. return get_val(bc, 1);
  127. }
  128. /* Return n bits from the buffer. n has to be in the 0-63 range. */
  129. static inline uint64_t bitstream_read_63(BitstreamContext *bc, unsigned n)
  130. {
  131. uint64_t ret = 0;
  132. #ifdef BITSTREAM_READER_LE
  133. uint64_t left = 0;
  134. #endif
  135. if (!n)
  136. return 0;
  137. if (n > bc->bits_left) {
  138. n -= bc->bits_left;
  139. #ifdef BITSTREAM_READER_LE
  140. left = bc->bits_left;
  141. #endif
  142. ret = get_val(bc, bc->bits_left);
  143. refill_64(bc);
  144. }
  145. #ifdef BITSTREAM_READER_LE
  146. ret = get_val(bc, n) << left | ret;
  147. #else
  148. ret = get_val(bc, n) | ret << n;
  149. #endif
  150. return ret;
  151. }
  152. /* Return n bits from the buffer. n has to be in the 0-32 range. */
  153. static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)
  154. {
  155. if (!n)
  156. return 0;
  157. if (n > bc->bits_left) {
  158. refill_32(bc);
  159. if (bc->bits_left < 32)
  160. bc->bits_left = n;
  161. }
  162. return get_val(bc, n);
  163. }
  164. /* Return n bits from the buffer as a signed integer.
  165. * n has to be in the 0-32 range. */
  166. static inline int32_t bitstream_read_signed(BitstreamContext *bc, unsigned n)
  167. {
  168. return sign_extend(bitstream_read(bc, n), n);
  169. }
  170. static inline unsigned show_val(const BitstreamContext *bc, unsigned n)
  171. {
  172. #ifdef BITSTREAM_READER_LE
  173. return bc->bits & ((UINT64_C(1) << n) - 1);
  174. #else
  175. return bc->bits >> (64 - n);
  176. #endif
  177. }
  178. /* Return n bits from the buffer, but do not change the buffer state.
  179. * n has to be in the 0-32 range. */
  180. static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)
  181. {
  182. if (n > bc->bits_left)
  183. refill_32(bc);
  184. return show_val(bc, n);
  185. }
  186. /* Return n bits from the buffer as a signed integer, but do not change the
  187. * buffer state. n has to be in the 0-32 range. */
  188. static inline int bitstream_peek_signed(BitstreamContext *bc, unsigned n)
  189. {
  190. return sign_extend(bitstream_peek(bc, n), n);
  191. }
  192. static inline void skip_remaining(BitstreamContext *bc, unsigned n)
  193. {
  194. #ifdef BITSTREAM_READER_LE
  195. bc->bits >>= n;
  196. #else
  197. bc->bits <<= n;
  198. #endif
  199. bc->bits_left -= n;
  200. }
  201. /* Skip n bits in the buffer. */
  202. static inline void bitstream_skip(BitstreamContext *bc, unsigned n)
  203. {
  204. if (n <= bc->bits_left)
  205. skip_remaining(bc, n);
  206. else {
  207. n -= bc->bits_left;
  208. skip_remaining(bc, bc->bits_left);
  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 the LUT element for the given bitstream configuration. */
  245. static inline int set_idx(BitstreamContext *bc, int code, int *n, int *nb_bits,
  246. VLC_TYPE (*table)[2])
  247. {
  248. unsigned idx;
  249. *nb_bits = -*n;
  250. idx = bitstream_peek(bc, *nb_bits) + code;
  251. *n = table[idx][1];
  252. return table[idx][0];
  253. }
  254. /**
  255. * Parse a VLC code.
  256. * @param bits is the number of bits which will be read at once, must be
  257. * identical to nb_bits in init_vlc()
  258. * @param max_depth is the number of times bits bits must be read to completely
  259. * read the longest VLC code
  260. * = (max_vlc_length + bits - 1) / bits
  261. * If the VLC code is invalid and max_depth = 1, then no bits will be removed.
  262. * If the VLC code is invalid and max_depth > 1, then the number of bits removed
  263. * is undefined. */
  264. static inline int bitstream_read_vlc(BitstreamContext *bc, VLC_TYPE (*table)[2],
  265. int bits, int max_depth)
  266. {
  267. int nb_bits;
  268. unsigned idx = bitstream_peek(bc, bits);
  269. int code = table[idx][0];
  270. int n = table[idx][1];
  271. if (max_depth > 1 && n < 0) {
  272. skip_remaining(bc, bits);
  273. code = set_idx(bc, code, &n, &nb_bits, table);
  274. if (max_depth > 2 && n < 0) {
  275. skip_remaining(bc, nb_bits);
  276. code = set_idx(bc, code, &n, &nb_bits, table);
  277. }
  278. }
  279. skip_remaining(bc, n);
  280. return code;
  281. }
  282. #define BITSTREAM_RL_VLC(level, run, bc, table, bits, max_depth) \
  283. do { \
  284. int n, nb_bits; \
  285. unsigned index = bitstream_peek(bc, bits); \
  286. level = table[index].level; \
  287. n = table[index].len; \
  288. \
  289. if (max_depth > 1 && n < 0) { \
  290. bitstream_skip(bc, bits); \
  291. \
  292. nb_bits = -n; \
  293. \
  294. index = bitstream_peek(bc, nb_bits) + level; \
  295. level = table[index].level; \
  296. n = table[index].len; \
  297. if (max_depth > 2 && n < 0) { \
  298. bitstream_skip(bc, nb_bits); \
  299. nb_bits = -n; \
  300. \
  301. index = bitstream_peek(bc, nb_bits) + level; \
  302. level = table[index].level; \
  303. n = table[index].len; \
  304. } \
  305. } \
  306. run = table[index].run; \
  307. bitstream_skip(bc, n); \
  308. } while (0)
  309. /* Return decoded truncated unary code for the values 0, 1, 2. */
  310. static inline int bitstream_decode012(BitstreamContext *bc)
  311. {
  312. if (!bitstream_read_bit(bc))
  313. return 0;
  314. else
  315. return bitstream_read_bit(bc) + 1;
  316. }
  317. /* Return decoded truncated unary code for the values 2, 1, 0. */
  318. static inline int bitstream_decode210(BitstreamContext *bc)
  319. {
  320. if (bitstream_read_bit(bc))
  321. return 0;
  322. else
  323. return 2 - bitstream_read_bit(bc);
  324. }
  325. /* Read sign bit and flip the sign of the provided value accordingly. */
  326. static inline int bitstream_apply_sign(BitstreamContext *bc, int val)
  327. {
  328. int sign = bitstream_read_signed(bc, 1);
  329. return (val ^ sign) - sign;
  330. }
  331. /* Unwind the cache so a refill_32 can fill it again. */
  332. static inline void bitstream_unwind(BitstreamContext *bc)
  333. {
  334. int unwind = 4;
  335. int unwind_bits = unwind * 8;
  336. if (bc->bits_left < unwind_bits)
  337. return;
  338. bc->bits >>= unwind_bits;
  339. bc->bits <<= unwind_bits;
  340. bc->bits_left -= unwind_bits;
  341. bc->ptr -= unwind;
  342. }
  343. /* Unget up to 32 bits. */
  344. static inline void bitstream_unget(BitstreamContext *bc, uint64_t value,
  345. size_t amount)
  346. {
  347. size_t cache_size = sizeof(bc->bits) * 8;
  348. if (bc->bits_left + amount > cache_size)
  349. bitstream_unwind(bc);
  350. bc->bits = (bc->bits >> amount) | (value << (cache_size - amount));
  351. bc->bits_left += amount;
  352. }
  353. #endif /* AVCODEC_BITSTREAM_H */